﻿#include "stdafx.h"
#ifdef LK_
#include<stdlib.h>
#include <float.h>
#include<string.h>
#include<locale.h>
#include<locale>
namespace LK
{
	LPCWSTR String::ppNumWChs[ChsNumMaxCount] =
	{ L"0零〇０",
		L"1一壹①㈠⑴⒈Ⅰⅰ１", L"2二两贰②㈡⑵⒉Ⅱⅱ２貮",
		L"3三叁③㈢⑶⒊Ⅲⅲ３", L"4四肆④㈣⑷⒋Ⅳⅳ４",
		L"5五伍⑤㈤⑸⒌Ⅴⅴ５", L"6六陆⑥㈥⑹⒍Ⅵⅵ６",
		L"7七柒⑦㈦⑺⒎Ⅶⅶ７", L"8八捌⑧㈧⑻⒏Ⅷⅷ８",
		L"9九玖⑨㈨⑼⒐Ⅸⅸ９", L"⑩㈩⑽⒑Ⅹⅹ",
		L"⑾⒒", L"⑿⒓", L"⒀⒔", L"⒁⒕", L"⒂⒖", L"⒃⒗", L"⒄⒘", L"⒅⒙", L"⒆⒚", L"⒇⒛"
	};//记录数字字符
	unsigned char String::pNumCounts[ChsNumMaxCount] = { 4, 10, 12, 10, 10, 10, 10, 10, 10, 10, 6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };//记实每个数字字符数
	LPCWSTR String::ppUnitWChs[9] = { L"十拾", L"廿念", L"百佰", L"千仟kK", L"万", L"兆mM", L"亿", L"吉gG", L"太tT" };//记录单位字符
	unsigned long long String::pUnitValue[9] = { 10, 20, 100, 1000, 10000, 1000000, 100000000, 1000000000, 1000000000000 };//记录每个单位对应的数值
	unsigned char String::pUnitCounts[9] = { 2, 2, 2, 4, 1, 3, 1, 3, 3 };//记录每个单位对应的字符数量

	char const* String::localinitonce()
	{
		static char const* p;
		if (p)return p;
#ifdef LK_MUTEX_
		Mutex::GetDefaultRef().Lock();
#endif
		if (!p) { p = localinit(); }
#ifdef LK_MUTEX_
		Mutex::GetDefaultRef().UnLock();
#endif
		return p;
	}
	char const* String::localinit()
	{
		static std::locale loc = std::locale("");
		std::cout.imbue(loc);
		std::wcout.imbue(loc);
		std::cin.imbue(loc);
		std::wcin.imbue(loc);
		std::cerr.imbue(loc);
		return loc.name().c_str();
	}

	size_t String::DefPrec = 6;

	void String::Resize(size_t s)
	{
		if (s < MaxSize) { Length = s; return; }
		if (type != Normal)Throw<PIndexOutOfRangeException>(L"引用字符串不能调整内存大小");
		size_t maxs;
		LPWSTR p = Memory::New<WCHAR>(s + 1, &maxs);
		Memory::Copy(p, wString, sizeof(WCHAR)*(Length + 1));
		Clear();
		wString = p;
		Length = s;
		const_cast<size_t&>(MaxSize) = maxs;
	}
	void String::ResizeAndDestroy(size_t s)
	{
		if (s < MaxSize) { Length = s; return; }
		if (type != Normal)Throw<PIndexOutOfRangeException>(L"引用字符串不能调整内存大小");
		Clear();
		LPWSTR p = Memory::New<WCHAR>(s + 1, &MaxSize);
		wString = p;
		Length = s;
	}
	index_t String::Strlen(LPCSTR str)
	{
		if (str == NULL)return 0;
		if (sizeof(char)>2)
		{
			index_t i = 0;
			while (str[i] != 0)
				++i;
			return i;
		}
		static const size_t magic_bits = size_t(sizeof(char) == 1 ? (0xfefefefefefefeff ^ (1ull << (sizeof(size_t) * 8 - 1))) : (0xfffefffefffeffff ^ (1ull << (sizeof(size_t) * 8 - 1))));
		size_t* sp = (size_t*)str;
		switch ((size_t)str & (sizeof(size_t) - 1))
		{
		case sizeof(char) : if (*((char*)sp) == 0)return 0; ++((char*&)sp);
		case sizeof(char) * 2:if (*((char*)sp) == 0)return (char*)sp - str; ++((char*&)sp);
		case sizeof(char) * 3:if (*((char*)sp) == 0)return (char*)sp - str; ++((char*&)sp);
#ifdef _X64
		case sizeof(char) * 4:if (*((char*)sp) == 0)return (char*)sp - str; ++((char*&)sp);
		case sizeof(char) * 5:if (*((char*)sp) == 0)return (char*)sp - str; ++((char*&)sp);
		case sizeof(char) * 6:if (*((char*)sp) == 0)return (char*)sp - str; ++((char*&)sp);
		case sizeof(char) * 7:if (*((char*)sp) == 0)return (char*)sp - str; ++((char*&)sp);
#endif
		default:break;
		}
		for (;;) {
			size_t longword = *sp;
			if ((((longword + magic_bits) ^ ~longword) & ~magic_bits) != 0)
			{
				const char*cp = (const char*)(sp);
				if (cp[0] == 0)
					return cp - str;
				if (cp[1] == 0)
					return cp - str + 1;
				if (cp[2] == 0)
					return cp - str + 2;
				if (cp[3] == 0)
					return cp - str + 3;
#ifdef _X64
				if (cp[4] == 0)
					return cp - str + 4;
				if (cp[5] == 0)
					return cp - str + 5;
				if (cp[6] == 0)
					return cp - str + 6;
				if (cp[7] == 0)
					return cp - str + 7;
#endif
			}
			++sp;
		}

	}
	index_t String::Strlen(LPCWSTR str)
	{
		if (str == NULL)return 0;
		if (sizeof(wchar_t) != 2)
		{
			index_t i = 0;
			while (str[i] != 0)
				++i;
			return i;
		}
#ifdef _X64
		static const size_t magic_bits = 0xfffefffefffeffff ^ (1ull << (sizeof(size_t) * 8 - 1));
#endif
		size_t* sp = (size_t*)str;
		switch ((size_t)str & (sizeof(size_t) - 1))
		{
		case 2:if (*((wchar_t*)sp) == 0)return 0; ++((wchar_t*&)sp);
#ifdef _X64
		case 4:if (*((wchar_t*)sp) == 0)return (wchar_t*)sp - str; ++((wchar_t*&)sp);
		case 6:if (*((wchar_t*)sp) == 0)return (wchar_t*)sp - str; ++((wchar_t*&)sp);
#endif
		default:break;
		}
		for (;;) {
#ifdef _X64
			size_t longword = *sp;
			if ((((longword + magic_bits) ^ ~longword) & ~magic_bits) != 0)
			{
				const wchar_t*cp = (const wchar_t*)(sp);
				if (cp[0] == 0)
					return cp - str;
				if (cp[1] == 0)
					return cp - str + 1;
				if (cp[2] == 0)
					return cp - str + 2;
				if (cp[3] == 0)
					return cp - str + 3;
			}
#else
			if ((*sp & 0xffff) == 0)return (const wchar_t*)(sp)-str;
			if (*sp <= 0xffff)return (const wchar_t*)(sp)-str + 1;
#endif
			++sp;
		}

	}
	int String::WriteNum(long long v, wchar_t*p,int minlen,int d)
	{
		if (v < 0) { *p++ = L'-'; return WriteUNum(-v,p,minlen-1,d)+1; }
		return WriteUNum(v, p, minlen,d);
	}
	int String::WriteUNum(unsigned long long val, wchar_t*p,int minlen,int d)
	{
		int result = 0;
		do {
			p[result] = (wchar_t)(val % d);
			if (p[result] > 9)p[result] += L'A' - 10;
			else p[result] += L'0';
			++result;
			val /= d;
		} while (val != 0);
		while (result < minlen) p[result++] = L'0';
		for (int i = 0; i < result / 2; ++i)
			Swap(p[i], p[result - i - 1]);
		return result;
	}
	/**
	@brief 将val转换为字符串并写入p所指定的内存中，采用指数方式，并返回写入长度，不足minlen时小数点后补0，d为进制数*/
	int String::WriteFloatE(double val, wchar_t*p, int prec, int minlen, int d)
	{
		if (val == 0) { *p = L'0'; return 1; }
		if ( val!=val) { *p = L'#'; return 1; }
		bool nagtive = false;
		if (val < 0) { val = -val; if (minlen != 0)minlen--; *p++ = L'-'; nagtive = true; }
		int exp = (int)(log(val) / log((double)d));
		if(exp!=0&&-exp==exp) { *p = L'∞'; return 1; }
		val *= pow((double)d, -exp);
		if (val >= d) { val /= d; exp++; }
		else if (val < 1) { val *= d; exp--; }
		int len = 1;
		*p++ = (wchar_t)val;
		val = (val - (int)val)*d;
		if (prec > 1) {
			if (val != 0) {
				*p++ = L'.';
				do {
					*p++ = (wchar_t)val;
					val = (val - (int)val)*d;
					++len;
				} while (val != 0 && len < prec);
				if (val * 2 >= d) {//舍入
					wchar_t* pp = p - 1;
					while (*pp != L'.'&&++*pp == d) { *pp = 0; pp--; --p; --len; }
					if (*pp == L'.') { --p; --len; if (++*--pp == d) { *pp = 1;  exp++; --p; } }
				}
				if (len != 1)++len;
			}
			for (wchar_t* pp = p - len; pp != p; ++pp){
				if (*pp == L'.')continue;
				else {
					if (*pp >= 10)*pp += L'A';
					else *pp += L'0';
				}
			}
		}
		wchar_t tmps[16];
		int testlen = WriteNum(exp, tmps, exp < 0 ? 2 : 1, d);
		while(len+ testlen+1<minlen) {if(len++!=1)*p++ = L'0';else *p++ = L'.'; }
		*p++ = L'e';
		for (minlen = 0; minlen < testlen; ++minlen)p[minlen] = tmps[minlen];
		return len + testlen + 1 + nagtive;
	}
	/**
	@brief 将val转换为字符串并写入p所指定的内存中，采用小数点方式，并返回写入长度，不足minlen时小数点后补0，d为进制数*/
	int String::WriteFloatP(double val, wchar_t*p, int prec, int minlen, int d){
		return 0;
	}
	/**
	@brief 将val转换为字符串并写入p所指定的内存中，自动采用指数或者小数点方式，并返回写入长度，不足minlen时小数点后补0，d为进制数*/
	int String::WriteFloat(double val, wchar_t*p, int prec, int minlen, int d) {
		return 0;
	}

	LPWSTR String::StrRepeat(LPCWSTR wString, index_t count, index_t*outlength, size_t*outsize)
	{//验证
		if (wString == NULL)return NULL;
		if (count < 0)Throw<PInvalidArgumentException>();//参数无效
		index_t len = Strlen(wString);
		LPWSTR p = Memory::New<WCHAR>(len*count + 1, outsize);
		if (outlength)*outlength = len*count;
		p[len*count] = 0;
		while (count--)Memory::Copy(p + count*len, wString, len*sizeof(WCHAR));
		return p;
	}
	LPSTR String::StrRepeat(LPCSTR mString, index_t count, index_t*outlength, size_t*outsize)
	{
		if (mString == NULL)return NULL;
		if (count < 0)Throw<PInvalidArgumentException>();//参数无效
		index_t len = Strlen(mString);
		LPSTR p = Memory::New<CHAR>(len*count + 1, outsize);
		if (outlength)*outlength = len*count;
		p[len*count] = 0;
		while (count--)Memory::Copy(p + count*len, mString, len);
		return p;
	}
	LPWSTR String::StrRepeat(LPCWSTR wString, index_t length, index_t count, size_t*outsize)
	{
		if (wString == NULL)return NULL;
		if (count < 0 || length < 0)Throw<PInvalidArgumentException>();//参数无效
		LPWSTR p = Memory::New<WCHAR>(length*count + 1, outsize);
		p[length*count] = 0;
		while (count--)Memory::Copy(p + count*length, wString, length*sizeof(WCHAR));
		return p;
	}
	LPSTR String::StrRepeat(LPCSTR mString, index_t length, index_t count, size_t*outsize)
	{
		if (mString == NULL)return NULL;
		if (count < 0 || length < 0)Throw<PInvalidArgumentException>();//参数无效
		LPSTR p = Memory::New<CHAR>(length*count + 1, outsize);
		p[length*count] = 0;
		while (count--)Memory::Copy(p + count*length, mString, length);
		return p;
	}
	//多字节转换为Unicode，length表示的是source除终止字符'\0'的字符数（字节数），小玉0代表自动计算，返回转换后的长度
	LPWSTR String::AnsiToUnicode(LPWSTR tmp, LPCSTR source, index_t* outLength, size_t*outsize, index_t length)
	{
		String::localinitonce();
		if (length < 0)length = (index_t)strlen(source);
		index_t len = 0;
		for (index_t i = 0; i < length; ++i, ++len)//统计字符数
			if (source[i] < 0)++i;
		size_t mSize = 0;
		if (tmp == NULL)
		{
			tmp = Memory::New<WCHAR>(len + 2, &mSize);
		}
		else if (outsize)
		{
			if ((index_t)*outsize < len + 2)
			{
				delete[] tmp;
				tmp = Memory::New<WCHAR>(len + 2, &mSize);
			}
		}
#ifdef _WINDOWS
		if (len)len = MultiByteToWideChar(CP_ACP, 0, source, (int)length, tmp, (int)(len + 1));
#else
		if (len)mbstowcs(tmp, source, length);
#endif
		tmp[len] = 0;
		if (outLength)*outLength = len;
		if (outsize&&mSize != 0)*outsize = mSize;
		return tmp;
	}
	LPWSTR String::AnsiToUnicode(LPCSTR source, index_t* outLength, size_t*outsize, index_t length)
	{
		return AnsiToUnicode(NULL, source, outLength, outsize, length);
	}
	LPWSTR String::AnsiToUnicode(LPCSTR source, index_t*outlength, index_t length)
	{
		return AnsiToUnicode(source, outlength, 0, length);
	}
	LPWSTR String::AnsiToUnicode(LPCSTR source, index_t length)
	{
		return AnsiToUnicode(source, 0, 0, length);
	}
	//Unicode转换为多字节，length表示的是source除终止字符'\0'的字符数(字节数/2)，返回转换后的长度
	LPSTR String::UnicodeToAnsi(LPCWSTR source, index_t* outLength, size_t* outsize, index_t length)
	{
		String::localinitonce();
		if (length < 0)length = Strlen(source);
		size_t len = length*sizeof(WCHAR);
		for (index_t i = 0; i < length; ++i)//统计字符数
			if ((source[i] & 0xffffff80) == 0)len -= (sizeof(WCHAR) - 1);
		//忽略需要三个及以上字节表示的字符
		LPSTR tmp = Memory::New<CHAR>(len + 2, outsize);
#ifdef _WINDOWS
		CHAR c = '_';//无法转换的字符使用下划线替换
		if (len)len = WideCharToMultiByte(CP_ACP, 0, source, (int)length, tmp, (int)len + 1, &c, NULL);
#else
		if (len)len = wcstombs(tmp, source, len);
#endif
		tmp[len] = 0;
		if (outLength)*outLength = len;
		return tmp;
	}
	LPSTR String::UnicodeToAnsi(LPCWSTR source, index_t* outLength, index_t length)
	{
		return UnicodeToAnsi(source, outLength, 0, length);
	}
	LPSTR String::UnicodeToAnsi(LPCWSTR source, index_t length)
	{
		return UnicodeToAnsi(source, 0, 0, length);
	}
	//查看vString2在vString1的位置
	index_t String::Find(const String& vString1, index_t index1, const String& vString2, index_t index2, index_t maxLength1, index_t maxLength2, bool ignoreCase)
	{
		if (index1 < 0 || index2 < 0)Throw<PInvalidArgumentException>();//参数无效
		LPWSTR tmp1 = vString1.wString + index1;
		LPWSTR tmp2 = vString2.wString + index2;
		index_t tmp;
		if (maxLength1 < 0)maxLength1 = vString1.Length-index1;
		if (maxLength2 < 0)maxLength2 = vString2.Length - index2;
		if (ignoreCase)
		{
			tmp1 = String::Strcpy(tmp1, maxLength1);
			tmp2 = String::Strcpy(tmp2, maxLength2);
			if (maxLength1)String::wcslwr(tmp1, maxLength1);
			if (maxLength2)String::wcslwr(tmp2, maxLength2);
		}
		if (index1 + maxLength1 == vString1.Length&&index2 + maxLength2 == vString2.Length) {
			wchar_t const*p = wcsstr(tmp1, tmp2);
			tmp = p ? (index_t)(p - vString1.wString) : -1;
		}
		else tmp = String::Strstr(tmp1, tmp2, maxLength1, maxLength2);
		if (ignoreCase)
		{
			delete[]tmp1;
			delete[]tmp2;
		}
		if(tmp < 0)return tmp;
		return tmp + index1;
	}
	//查找vString2在vString1最后出现的位置
	index_t String::Last(const String& vString1, index_t index1, const String& vString2, index_t index2, index_t maxLength1, index_t maxLength2, bool ignoreCase)
	{
		if (index1 < 0 || index2 < 0)Throw<PInvalidArgumentException>();//参数无效
		LPCWSTR tmp1 = vString1.wString + index1;
		LPCWSTR tmp2 = vString2.wString + index2;
		if (maxLength1 < 0)maxLength1 = String::Strlen(tmp1);
		else maxLength1 -= index1;
		if (maxLength2 < 0)maxLength2 = String::Strlen(tmp2);
		else maxLength2 -= index2;
		index_t tmp;
		for (tmp = maxLength1 - maxLength2; tmp >= 0; --tmp)
		{
			if (tmp1[tmp] == *tmp2)
			{
				if (!(ignoreCase ? &String::memicmp : &String::memcmp)(tmp1 + tmp, tmp2, maxLength2))break;
			}
		}
		if (tmp < 0)return -1;
		return tmp + index1;
	}
	//析构函数
	String::~String()
	{
		Clear();
	}
	//构造函数
	String::String() :Length(0), MaxSize(1),  mString(NULL), wString(&(*new WCHAR[1] = 0)), type(Normal)
	{       }
	//由数字创建字符串
	String::String(float number, size_t prec) : Length(0), MaxSize(0), mString(NULL), wString(NULL), type(Normal)
	{
		if (number * 2 == number)
		{
			wString = new WCHAR[4];
			const_cast<size_t&>(MaxSize) = 4;
			Length = 2;
			wString[0] = number>0 ? L'+' : L'-';
			wString[1] = L'∞';
			wString[2] = 0;
		}
		else if (number != number)
		{
			wString = new WCHAR[4];
			const_cast<size_t&>(MaxSize) = 4;
			Length = 2;
			wString[0] = L'异';
			wString[1] = L'常';
			wString[2] = 0;
		}
		else if (number != 0)
		{
			ResizeAndDestroy(32);
			//获取正负号记录negative
			bool negative = number < 0;
			if (negative)
			{
				number = -number;
				*wString++ = L'-';
			}
			//整数记号：
			bool it = (long)number == number;
			//获取10进制指数exp和有效数字number
			union
			{
				float fnum;
				s32 snum;
			};
			fnum = number;
			int exp = (int)(((snum >> 23) - 127) / 3.3219280948874);
			number = (float)(exp < 0 ? (number*IntPow10(-exp)) : (number / IntPow10(exp)));
			if (number < 1) { number *= 10; --exp; }
			else if (number >= 10) { number /= 10; ++exp; }
			//获取有效数字字符串wString和有数据字符串长度len
			int len = 0;
			while (len < 25 && number != 0)
			{
				wString[len++] = L'0' + (wchar_t)number;
				number = (number - (int)number) * 10;
			}
			//整数标记和精度限制处理
			if (it &&len>exp + 1 && ((index_t)prec>exp + 1 || prec == 0))prec = exp + 1;
			if (prec > 0 && (index_t)prec < len)
			{
				len = (int)prec;
				if (wString[len] >= L'5')//四舍五入
				{
					while (++wString[len - 1] > L'9')if (--len == 0)break;
					if (len == 0) { len = 1; wString[0] = L'1'; exp++; }
				}
			}

			//判断使用科学计数法还是直接输出
			int lene = (exp > 9 || exp < -9) + (exp < 0) + 1 + (len != 1);//科学计数法所需附加长度
			int lenp = (exp >= len ? exp - len + 1 : 0) + (exp + 1 < len) + (exp < 0 ? -exp : 0);//非科学计数法附加长度
			if (lenp <= lene)//非科学计数法
			{
				if (exp >= len)
					while (len <= exp)wString[len++] = L'0';
				else if (exp < 0)
				{
					for (int i = len - exp; i > -exp; --i)
						wString[i] = wString[i + exp - 1];
					for (int i = 0; i <= -exp; ++i)wString[i] = L'0';
					wString[1] = L'.';
					len -= exp - 1;
				}
				else if (exp + 1 < len)//需要加小数点
				{
					for (int i = len; i > exp; --i)
						wString[i] = wString[i - 1];
					wString[exp + 1] = L'.';
					++len;
				}
			}
			else//科学计数法
			{
				for (int i = len; i > 1; --i)wString[i] = wString[i - 1];
				if (len > 1)wString[1] = L'.';
				else --len;
				wString[++len] = L'e';
				if (exp < 0) { wString[++len] = L'-'; exp = -exp; }
				if (exp > 9)wString[++len] = L'0' + exp / 10;
				wString[++len] = L'0' + exp % 10;
				++len;
			}
			wString[len] = 0;
			if (negative)--wString;
			Length = len + negative;
		}
		else//为0
		{
			wString = new WCHAR[4];
			MaxSize = 4;
			Length = 1;
			wString[0] = L'0';
			wString[1] = 0;
		}
	}
	//由数字创建字符串
	String::String(double number, size_t prec) :Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		if (number * 2 == number)
		{
			wString = new WCHAR[4];
			const_cast<size_t&>(MaxSize) = 4;
			Length = 2;
			wString[0] = number>0?L'+':L'-';
			wString[1] = L'∞';
			wString[2] = 0;
		}
		else if (number != number)
		{
			wString = new WCHAR[4];
			const_cast<size_t&>(MaxSize) = 4;
			Length = 2;
			wString[0] = L'异';
			wString[1] = L'常';
			wString[2] = 0;
		}
		else if (number != 0)
		{
			ResizeAndDestroy(64);
			//获取正负号记录negative
			bool negative = number < 0;
			if (negative)
			{
				number = -number;
				*wString++ = L'-';
			}
			//整数记号：
			bool it = (long long)number == number;
			//获取10进制指数exp和有效数字number
			union
			{
				double dnum;
				s64 snum;
			};
			dnum = number;
			int exp = (int)(((snum >> 52) - 1023) / 3.3219280948875);
			number = (exp < 0 ? (number*IntPow10(-exp)) : (number / IntPow10(exp)));
			if (number < 1) { number *= 10; --exp; }
			else if (number >= 10) { number /= 10; ++exp; }
			//获取有效数字字符串wString和有数据字符串长度len
			int len = 0;
			while (len < 53 && number != 0)
			{
				wString[len++] = L'0' + (wchar_t)number;
				number = (number - (int)number) * 10;
			}
			//整数标记和精度限制处理
			if (it &&len>exp + 1 && ((index_t)prec>exp + 1 || prec == 0))prec = exp + 1;
			if (prec > 0 && (index_t)prec < len)
			{
				len = (int)prec;
				if (wString[len] >= L'5')//四舍五入
				{
					while (++wString[len - 1] > L'9')if (--len == 0)break;
					if (len == 0) { len = 1; wString[0] = L'1'; exp++; }
				}
			}
			//判断使用科学计数法还是直接输出
			int lene = (exp > 9 || exp < -9) + (exp < 0) + 1 + (len != 1);//科学计数法所需附加长度
			int lenp = (exp >= len ? exp - len + 1 : 0) + (exp + 1 < len) + (exp < 0 ? -exp : 0);//非科学计数法附加长度
			if (lenp <= lene)//非科学计数法
			{
				if (exp >= len)
					while (len <= exp)wString[len++] = L'0';
				else if (exp < 0)
				{
					for (int i = len - exp; i > -exp; --i)
						wString[i] = wString[i + exp - 1];
					for (int i = 0; i <= -exp; ++i)wString[i] = L'0';
					wString[1] = L'.';
					len -= exp - 1;
				}
				else if (exp + 1 < len)//需要加小数点
				{
					for (int i = len; i > exp; --i)
						wString[i] = wString[i - 1];
					wString[exp + 1] = L'.';
					++len;
				}
			}
			else//科学计数法
			{
				for (int i = len; i > 1; --i)wString[i] = wString[i - 1];
				if (len > 1)wString[1] = L'.';
				else --len;
				wString[++len] = L'e';
				if (exp < 0) { wString[++len] = L'-'; exp = -exp; }
				if (exp > 99)wString[++len] = L'0' + exp / 100;
				if (exp > 9)wString[++len] = L'0' + (exp / 10) % 10;
				wString[++len] = L'0' + exp % 10;
				++len;
			}
			wString[len] = 0;
			if (negative)--wString;
			Length = len + negative;
		}
		else//为0
		{
			wString = new WCHAR[4];
			MaxSize = 4;
			Length = 1;
			wString[0] = L'0';
			wString[1] = 0;
		}
	}
	//由数字创建字符串
	String::String(unsigned long long number) :Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(20);
		unsigned long long tmp;
		if (number != 0)
		{
			tmp = 10000000000000000000ull;
			while (tmp > number)tmp /= 10;
		}
		else tmp = 1;
		int len = 0;
		while (tmp != 0)
		{
			wString[len++] = L'0' + (WCHAR)(number / tmp);
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(unsigned long number) : Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(10);
		unsigned long tmp = 1000000000ul;
		while (tmp > number&&tmp != 1)tmp /= 10;
		int len = 0;
		while (tmp != 0)
		{
			wString[len++] = L'0' + (WCHAR)(number / tmp);
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(unsigned int number) :Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(10);
		unsigned int tmp = 1000000000ul;
		if (number != 0)
		{
			tmp = 1000000000ul;
			while (tmp > number)tmp /= 10;
		}
		else tmp = 1;
		int len = 0;
		while (tmp != 0)
		{
			wString[len++] = L'0' + number / tmp;
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(long long number) : Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(21);
		int len = 0;
		if (number < 0) { number = -number; wString[len++] = L'-'; }
		unsigned long long tmp;
		if (number != 0)
		{
			tmp = 10000000000000000000ull;
			while (tmp > (unsigned long long)number)tmp /= 10;
		}
		else tmp = 1;
		while (tmp != 0)
		{
			wString[len++] = L'0' + (WCHAR)(number / tmp);
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(long number) : Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(11);
		int len = 0;
		if (number < 0) { number = -number; wString[len++] = L'-'; }
		unsigned long tmp;
		if (number != 0)
		{
			tmp = 1000000000ul;
			while (tmp > (unsigned long)number)tmp /= 10;
		}
		else tmp = 1;
		while (tmp != 0)
		{
			wString[len++] = L'0' + (WCHAR)(number / tmp);
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(int number) : Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(11);
		int len = 0;
		if (number < 0) { number = -number; wString[len++] = L'-'; }
		unsigned int tmp;
		if (number != 0)
		{
			tmp = 1000000000ul;
			while (tmp > (unsigned int)number)tmp /= 10;
		}
		else tmp = 1;
		while (tmp != 0)
		{
			wString[len++] = L'0' + number / tmp;
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(unsigned short number) :Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(5);
		unsigned short tmp;
		if (number != 0)
		{
			tmp = 10000;
			while (tmp > number)tmp /= 10;
		}
		else tmp = 1;
		int len = 0;
		while (tmp != 0)
		{
			wString[len++] = L'0' + number / tmp;
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(short number) :Length(0), MaxSize(0), mString(NULL), wString(NULL), type(Normal)
	{
		ResizeAndDestroy(6);
		unsigned short tmp;
		if (number != 0)
		{
			tmp = 10000;
			while (tmp > (unsigned short)number)tmp /= 10;
		}
		else tmp = 1;
		int len = 0;
		while (tmp != 0)
		{
			wString[len++] = L'0' + number / tmp;
			number %= tmp;
			tmp /= 10;
		}
		wString[len] = 0;
		Length = len;
	}
	//由数字创建字符串
	String::String(unsigned char number) :Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		wString = new WCHAR[4];
		MaxSize = 4;
		Length = 1;
		wString[0] = number;
		wString[1] = 0;
	}
	//由数字创建字符串
	String::String(signed char number) :Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		wString = new WCHAR[4];
		MaxSize = 4;
		Length = 1;
		wString[0] = number;
		wString[1] = 0;
	}
	String::String(ptr<Object> const& str, index_t count)
		: Length(str.GetPtr() ? str->ToString()->Length*count : RThrow<index_t,PPointerIsNullException >()),
		MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		wString = StrRepeat(str.GetPtr() ? str->ToString()->wString : RThrow<wchar_t*,PPointerIsNullException >(), str.GetPtr() ? str->ToString()->Length : RThrow<index_t,PPointerIsNullException >(), count, const_cast<size_t*>(&MaxSize));
	}
	//由count个LPSTR构造
	String::String(LPCSTR str, index_t count)
		: Length(count < 0 ? RThrow<index_t,PInvalidArgumentException>() : 0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		LPWSTR p = NULL;
		index_t len = 0;
		if (str)p = AnsiToUnicode(str, &len);
		wString = StrRepeat(p, len, count, const_cast<size_t*>(&MaxSize));
		Length = len*count;
		delete[]p;
	}
	String::String(CHAR c, index_t count)
		:Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		if (count < 0)Throw<PInvalidArgumentException>();
		ResizeAndDestroy(count);
		wmemset(wString, c, count);
		wString[count] = 0;
	}
	String::String(WCHAR wc, index_t count)
		: Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		if (count < 0)Throw<PInvalidArgumentException>();
		ResizeAndDestroy(count);
		wmemset(wString, wc, count);
		wString[count] = 0;
	}
	String::String(PWCharArray wstr, index_t count)
		: Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		if (count < 0)Throw<PInvalidArgumentException>();
		index_t len = 0;
		while (len < wstr->size() && wstr[len])++len;
		wString = StrRepeat(wstr, len, count, const_cast<size_t*>(&MaxSize));
		Length = len*count;
	}
	String::String(PCharArray mstr, index_t count)
		: Length(0), MaxSize(0),mString(NULL), wString(NULL), type(Normal)
	{
		if (count < 0)Throw<PInvalidArgumentException>();
		index_t len = Strlen(mstr);
		if (len > mstr->size())len = mstr->size();
		LPWSTR wstr = AnsiToUnicode(mstr, &len, len);
		Length = len*count;
		wString = StrRepeat(wstr, len, count, const_cast<size_t*>(&MaxSize));
		delete[]wstr;
	}
	const WCHAR String::operator[](index_t index)const
	{
		if (index < 0)Throw<PInvalidArgumentException>();
		if (index > Length)Throw<PIndexOutOfRangeException>();
		return wString[index];
	}
	void String::SetChar(index_t index, WCHAR wc)
	{
		if (type == ReadOnlyReference)Throw<PException >(L"字符串只读");
		if (index < 0)Throw<PInvalidArgumentException>();
		if (index > Length)Throw<PIndexOutOfRangeException>();
		if (wString[index] == wc)return;
		delete[]ChangeAnsi(NULL);
		wString[index] = wc;
		if (wc == 0)
		{
			Length = index;
		}
	}


	String& String::operator=(const String& vString)//赋值运算
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		delete[]ChangeAnsi(NULL);
		if (MaxSize > (size_t)vString.Length)
		{
			Memory::Copy(wString, vString.wString, (vString.Length + 1)*sizeof(WCHAR));
		}
		else
		{
			ResizeAndDestroy(vString.Length);
			Memory::Copy(wString, vString.wString, (vString.Length + 1)*sizeof(WCHAR));
		}
		Length = vString.Length;
		return *this;
	}
	String& String::operator=(LPCSTR mstr)
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		delete[]ChangeAnsi(NULL);
		wString = AnsiToUnicode(wString, mstr, &Length, const_cast<size_t*>(&MaxSize));
		mString = NULL;
		return *this;
	}
	String& String::operator=(LPCWSTR wstr)
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		delete[]ChangeAnsi(NULL);
		ResizeAndDestroy(Strlen(wstr));
		Memory::Copy(wString, wstr, Length*sizeof(wchar_t) + sizeof(WCHAR));
		mString = NULL;
		return *this;
	}
	String& String::operator+=(const String& vString)//串接运算
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		delete[]ChangeAnsi(NULL);
		mString = NULL;
		Resize(Length + vString.Length);
		Memory::Copy(wString + Length - vString.Length, vString.wString, (vString.Length + 1)*sizeof(WCHAR));
		return *this;
	}
	String& String::operator+=(LPCSTR mstr)
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		delete[]ChangeAnsi(NULL);
		mString = NULL;
		index_t length;
		LPWSTR p = AnsiToUnicode(mstr, &length);
		Resize(Length + length);
		Memory::Copy(wString + Length - length, p, length*sizeof(WCHAR));
		delete[]p;
		wString[Length] = 0;
		return *this;
	}
	String& String::operator+=(LPCWSTR wstr)
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		delete[]ChangeAnsi(NULL);
		index_t length = Strlen(wstr);
		Resize(Length + length);
		Memory::Copy(wString + Length - length, wstr, length*sizeof(WCHAR));
		wString[Length] = 0;
		return *this;
	}
	/**
	@brief 将内部暂存的mString更改为指定的指针，并返回原mString，整个过程同步操作
	@warning 传入的指针必须通过new[]申请得到，并且交由String管理，返回的指针不使用时请使用delete[]删除*/
	LPSTR String::ChangeAnsi(LPSTR ms)const
	{
#ifdef _WINDOWS
		ms = (LPSTR)InterlockedExchangePointer((PVOID*)&const_cast<volatile LPSTR&>(mString), ms);
#else
		static Mutex m;
		m.Lock();
		Swap(const_cast<LPSTR&>(mString), ms);
		m.UnLock();
#endif
		return ms;
	}
	LPCSTR const String::ToAnsi()const//转换成原始的多字节字符串，const型
	{
		if (wString && !mString)//转换
		{
			LPSTR p = UnicodeToAnsi(wString, Length);
			delete[]ChangeAnsi(p);
		}
		return mString ? mString : "";
	}
	ptr<String> String::ToString()const
	{
		return const_cast<String*>(&(*this));
	}
	/**
	@brief 将p转换为一个无符号整数double。plen用以输出读取的的字符数,pdlen用以输出已读有效位数*/
	double String::ToUIntDouble(LPCWSTR lp, index_t* plen, index_t *pdlen)
	{
		double num = 0;
		LPCWSTR p = lp;
		LPCWSTR pd = NULL;
		while (*p >= L'0' && *p <= L'9'){//十进制表示
			if (!pd&&*p != L'0')pd = p;
			if (num > 1e17)break;
			num = num * 10 + *p - L'0';
			++p;
		}
		if (pd) {
			if (pdlen)*pdlen = p - pd;
			while (*p >= L'0' && *p <= L'9')++p;
		}
		else if (pdlen)*pdlen = p - lp;
		if (plen)*plen = p - lp;
		return num;
	}
	/**
	@brief 计算10的i次方,i的绝对值最大为308*/
	double String::IntPow10(index_t i)
	{
		if (i > DBL_MAX_10_EXP)return DBL_MAX;
		if (i < -DBL_MAX_10_EXP)return 0;
		static double pw10[1 + DBL_MAX_10_EXP] = { 0 };
		if (!*pw10) { *pw10 = 1; for (index_t k = 1; k <= DBL_MAX_10_EXP; ++k)pw10[k] = pw10[k - 1] * 10; }
		return i >= 0 ? pw10[i] : 1 / pw10[-i];
	}
	unsigned long long String::GetChsValue(unsigned char* ptype, unsigned long long* pvalue, size_t len)const
	{
		unsigned long long result(0);
		size_t lastpt(len);//指针指向后一个单位
		if (len > 1 && 0 == ptype[len - 1] && 1 == ptype[len - 2] && pvalue[len - 2] >= 100)//关注如二百五的情况
		{
			pvalue[len - 1] *= pvalue[len - 2] / 10;
		}
		for (index_t i(len - 1); i >= 0; --i)//从后向前扫描
		{
			if (1 == ptype[i])
			{
				if (lastpt < len && pvalue[i] <= pvalue[lastpt])//需要递归
				{
					index_t j(i - 1);
					while (j >= 0 && pvalue[j] <= pvalue[lastpt])--j;//寻找大单位，两个大单位之间递归
					if (j<0 || (1 == ptype[j] && pvalue[j]>pvalue[lastpt]))++j;
					pvalue[j] = GetChsValue(ptype + j, pvalue + j, lastpt - j);
					ptype[j] = 0;//变成数字处理
					for (size_t k = 1; lastpt + k <= len; ++k)
					{
						ptype[j + k] = ptype[lastpt + k - 1];
						pvalue[j + k] = pvalue[lastpt + k - 1];
					}
					len -= lastpt - 1 - j;
					lastpt = j + 1;
					i = j;
				}
				else
				{
					lastpt = i;
				}
			}
		}
		//扫描完毕，开始计算
		for (size_t i = 0; i < len; ++i)
		{
			if (1 == ptype[i])//遇到单位
			{
				if (i)
				{
					if (1 == ptype[i - 1] && 10 == pvalue[i] && 20 == pvalue[i - 1])//非常规处理：“廿十”
						continue;
					if (1 == ptype[i - 1] || !pvalue[i - 1])//非常规处理：单位紧挨如“千百”，或数值为0如“万零百”
						result += pvalue[i];
					else
						result += pvalue[i - 1] * pvalue[i];//正常赋值
				}
				else//单位打头
					result = pvalue[i];
			}
			else if (i && !ptype[i - 1])//遇到数字叠加
				pvalue[i] += pvalue[i - 1] * 10;
		}
		if (!ptype[len - 1])
			result += pvalue[len - 1];
		return result;
	}
	unsigned long long String::ChsToUInt64(LPCWSTR wstr, size_t len)const
	{
		if (!wstr)wstr = wString;
		if (!len)len = Length;
		unsigned char* ptype = new unsigned char[len];//0代表数字，1代表单位
		unsigned long long* pvalue = new unsigned long long[len];//0~9代表0到9
																 //数字字符串信息记录
		for (size_t k(0); k < len; ++k)
		{
			size_t i(0);
			while (i < ChsNumMaxCount)//支持的数字字符查找
			{
				size_t j(0);
				while (j < pNumCounts[i])
				{
					if (ppNumWChs[i][j] == wstr[k])
					{
						ptype[k] = 0;
						pvalue[k] = i;
						break;
					}
					++j;
				}
				if (j < pNumCounts[i])break;
				++i;
			}
			if (ChsNumMaxCount == i)//支持的单位字符查找
			{
				for (i = 0; i < ChsUnitMaxCount; ++i)
				{
					size_t j(0);
					while (j < pUnitCounts[i])
					{
						if (wstr[k] == ppUnitWChs[i][j])
						{
							ptype[k] = 1;
							pvalue[k] = pUnitValue[i];
							break;
						}
						++j;
					}
					if (j < pUnitCounts[i])break;
				}
				if (ChsUnitMaxCount == i)//非数字非单位字符
				{
					len = k;
					break;
				}
			}
		}
		unsigned long long result = GetChsValue(ptype, pvalue, len);
		delete[]ptype;
		delete[]pvalue;
		return result;
	}
	long long String::ChsToInt64()const
	{
		bool nagtive = false;
		LPWSTR p = const_cast<LPWSTR>(wString);
		index_t len = Length;
		while (*p == L'+' || *p == L'-' || *p == L'正' || *p == L'负' || *p == L'＋' || *p == L'－')
		{
			if (*p == L'-' || *p == L'负' || *p == L'－')nagtive = !nagtive;
			++p;
			--len;
		}
		return nagtive ? -(long long)ChsToUInt64(p, len) : (long long)ChsToUInt64(p, len);
	}

	unsigned long long String::ToUInt64()const
	{
		bool nagtive = false;
		unsigned long long num = 0;
		LPCWSTR p = ToUnicode();
		while (*p == L'+' || *p == L'-')
		{
			if (*p == L'-')nagtive = !nagtive;
			++p;
		}
		if (*p == L'0' && (*(p + 1) == L'x' || *(p + 1) == L'X'))//十六进制表示
		{
			p += 2;
			while ((*p >= L'0' && *p <= L'9') || (*p >= L'A'&&*p <= L'F') || (*p >= L'a'&&*p <= L'f'))
			{
				num = num * 16 + ((*p >= L'0' && *p <= L'9') ? *p - L'0' : ((*p >= L'A'&&*p <= L'F') ? *p - L'A' + 10 : *p - L'a' + 10));
				++p;
			}
		}
		else    while (*p >= L'0' && *p <= L'9')//十进制表示
		{
			num = num * 10 + *p - L'0';
			++p;
		}
		return nagtive ? -(signed long long)num : num;
	}
	long long String::ToInt64()const
	{
		bool nagtive = false;
		long long num = 0;
		LPCWSTR p = ToUnicode();
		while (*p == L'+' || *p == L'-')
		{
			if (*p == L'-')nagtive = !nagtive;
			++p;
		}
		if (*p == L'0' && (*(p + 1) == L'x' || *(p + 1) == L'X'))//十六进制表示
		{
			p += 2;
			while ((*p >= L'0' && *p <= L'9') || (*p >= L'A'&&*p <= L'F') || (*p >= L'a'&&*p <= L'f'))
			{
				num = num * 16 + ((*p >= L'0' && *p <= L'9') ? *p - L'0' : ((*p >= L'A'&&*p <= L'F') ? *p - L'A' + 10 : *p - L'a' + 10));
				++p;
			}
		}
		else    while (*p >= L'0' && *p <= L'9')//十进制表示
		{
			num = num * 10 + *p - L'0';
			++p;
		}
		return nagtive ? -num : num;
	}
	double String::ToDouble()const
	{
		LPCWSTR p = ToUnicode();
		if (!p)return 0;
		bool nagtive = false;//负号
		while (*p == L'+' || *p == L'-'){
			if (*p == L'-')nagtive = !nagtive;
			++p;
		}
		while (*p == L'0'){++p;}
		index_t len;//记录已读数量
		index_t dlen;//记录精度数
		double before = ToUIntDouble(p, &len,&dlen);//获取小数点转换之前的部分
		double point=0;//记录小数部分
		index_t lenPoint=0;
		p += len;
		if (*p==L'.'){//继续获取小数部分
			p++;
			LPCWSTR lp = p;
			while (*p >= L'0' && *p <= L'9') {//十进制表示
				if (point > 1e17)break;
				point = point * 10 + *p - L'0';
				++p;
			}
			lenPoint = p - lp;
			while (*p >= L'0' && *p <= L'9')++p;
		}
		index_t exp=0;//记录指数
		if (*p==L'e' || *p == L'E'){//继续获取指数部分
			bool enagtive = false;//指数负号
			p++;
			while (*p == L'+' || *p == L'-'){
				if (*p == L'-')enagtive = !enagtive;
				++p;
			}
			while (*p >= L'0' && *p <= L'9') {//十进制表示
				if (exp > 65536)return enagtive?0:nagtive ? MinValues::Double : MaxValues::Double;
				exp = exp * 10 + *p - L'0';
				++p;
			}
			if (enagtive)exp = -exp;
		}
		before = before*String::IntPow10(exp + len - dlen)+point*String::IntPow10(exp - lenPoint);
		return nagtive ? -before : before;
	}
	bool operator==(const String& vString1, const String& vString2)//字符串比较
	{
		return !String::Strcmp(vString1.wString, vString2.wString);
	}
	bool operator!=(const String& vString1, const String& vString2)
	{
		return String::Strcmp(vString1.wString, vString2.wString) != 0;
	}
	bool operator>(const String& vString1, const String& vString2)
	{
		return String::Strcmp(vString1.wString, vString2.wString) > 0;
	}
	bool operator<(const String& vString1, const String& vString2)
	{
		return String::Strcmp(vString1.wString, vString2.wString) < 0;
	}
	bool operator>=(const String& vString1, const String& vString2)
	{
		return String::Strcmp(vString1.wString, vString2.wString) >= 0;
	}
	bool operator<=(const String& vString1, const String& vString2)
	{
		return String::Strcmp(vString1.wString, vString2.wString) <= 0;
	}

	String operator+(const String& vString1, const String& vString2)
	{
		size_t mSize;
		index_t len;
		LPWSTR p = String::Strcat(vString1.wString, vString2.wString, &len, &mSize);
		return String(len, p, NULL, mSize);
	}


	Collection::ArrayIterator< WCHAR> String::First() { return Collection::ArrayIterator< WCHAR>(wString); }
	Collection::ArrayIterator< WCHAR const> String::First()const { return Collection::ArrayIterator< WCHAR const>(wString); }
	Collection::ArrayIterator< WCHAR> String::Last() { return Collection::ArrayIterator< WCHAR>(wString ? wString + Length - 1 : NULL); }
	Collection::ArrayIterator< WCHAR const> String::Last()const { return Collection::ArrayIterator< WCHAR>(wString ? wString + Length - 1 : NULL); }
	Collection::ArrayIterator< WCHAR> String::End() { return Collection::ArrayIterator< WCHAR>(wString + Length); }
	Collection::ArrayIterator< WCHAR const> String::End()const { return Collection::ArrayIterator< WCHAR>(wString + Length); }


	//复制到多字节数组，返回复制的字节数，结尾的'\0'算一个字节
	index_t String::CopyTo(PCharArray& destination, index_t sourceByteIndex, index_t destinationByteIndex, index_t lengthOfByte)const
	{
		index_t maxlen = String::Strlen(ToAnsi());
		if (maxlen < 0)return 0;
		if (sourceByteIndex<0 || destinationByteIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (sourceByteIndex > maxlen || destinationByteIndex>destination->size())Throw<PIndexOutOfRangeException>();//越界
		if (lengthOfByte < 0)
		{
			lengthOfByte = (index_t)destination->size() - destinationByteIndex <= maxlen - sourceByteIndex ? destination->size() - destinationByteIndex : maxlen + 1 - sourceByteIndex;
		}
		if (!lengthOfByte)return 0;
		Memory::Copy(destination + destinationByteIndex, ToAnsi() + sourceByteIndex, lengthOfByte);
		return lengthOfByte;
	}
	//复制到Unicode数组，返回复制的字符数，结尾的'\0'算一个字符(两个字节)
	index_t String::CopyTo(PWCharArray destination, index_t sourceIndex, index_t destinationIndex, index_t length)const
	{
		if (sourceIndex<0 || destinationIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (sourceIndex > Length || destinationIndex>destination->size())Throw<PIndexOutOfRangeException>();//越界
		if (length < 0)
		{
			length = (index_t)destination->size() - destinationIndex <= Length - sourceIndex ? destination->size() - destinationIndex : Length + 1 - sourceIndex;
		}
		if (!length)return 0;
		Memory::Copy(destination + destinationIndex, ToUnicode() + sourceIndex, length*sizeof(wchar_t));
		return length;
	}
	bool String::Contains(const String& xString)const
	{
		return String::Find(*this, 0, xString, 0, Length, xString.Length, false) >= 0;
	}
	size_t String::CountOf(const String& str)const
	{
		size_t count = 0;
		for (index_t i = 0; i + str.Length <= Length;)
		{
			if (String::memcmp(wString + i, str.wString, str.Length) == 0)
			{
				i += str.Length;
				count++;
			}
			else ++i;
		}
		return count;
	}
	size_t String::CountOf(WCHAR wch)const
	{
		size_t count = 0;
		for (index_t i = 0; i < Length; ++i)
		{
			if (wString[i] == wch)
				count++;
		}
		return count;
	}
	bool String::EndsWith(const String& xString)const//判断该字符串是否以xString结尾
	{
		if (Length < xString.Length || !xString.Length)
			return false;
		return !String::memcmp(ToUnicode() + Length - xString.Length, xString.ToUnicode(), xString.Length);
	}
	bool String::StartsWith(const String& xString)const//判断该字符串是否以xString开头
	{
		if (Length < xString.Length)return false;
		return !String::memcmp(ToUnicode(), xString.ToUnicode(), xString.Length);
	}

	index_t String::IndexOf(WCHAR wc)const//查找指定 Unicode 字符在此字符串中的第一个匹配项的索引。
	{
		return String::Strchr(wString, wc);
	}
	index_t String::IndexOf(WCHAR wc, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex)Throw<PIndexOutOfRangeException>();//越界
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			wc &= 0xcf;
			index_t i = startIndex;
			index_t len = Length;
			LPCWSTR p = ToUnicode();
			while (i < len)
			{
				if (p[i] == wc || p[i] == wc + 32)break;
				++i;
			}
			if (i == len)return -1;
			return i;
		}
		index_t tmp = String::Strchr(ToUnicode() + startIndex, wc);
		if (tmp < 0)return tmp;
		return tmp + startIndex;
	}
	index_t String::IndexOf(WCHAR wc, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			wc &= 0xcf;
			index_t i = startIndex;
			index_t len = startIndex + length;
			LPCWSTR p = ToUnicode();
			while (i < len)
			{
				if (p[i] == wc || p[i] == wc + 32)break;
				++i;
			}
			if (i == len)return -1;
			return i;
		}
		index_t tmp = String::Strchr(ToUnicode() + startIndex, wc, length);
		if (tmp < 0)return tmp;
		return tmp + startIndex;
	}

	index_t String::IndexOf(const String& xString)const//查找指定 字符串在此字符串中的第一个匹配项的索引。
	{
		return String::Find(*this, 0, xString, 0, Length, xString.Length, false);
	}
	index_t String::IndexOf(const String& xString, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (Length < startIndex)Throw<PIndexOutOfRangeException>();//参数无效
		return String::Find(*this, startIndex, xString, 0, Length - startIndex, xString.Length, ignoreCase);
	}
	index_t String::IndexOf(const String& xString, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		return String::Find(*this, startIndex, xString, 0, length, xString.Length, ignoreCase);
	}
	//寻找wArray数组中任意字符在此字符串首次出现的位置（不包含结尾字符）
	index_t String::IndexOfAny(PWCharArray wArray, index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		if (length < 0)length = Length - startIndex;
		if (!length)return -1;
		const wchar_t* p = wArray;
		const wchar_t* pd = ToUnicode();
		index_t len = wArray->size();
		WCHAR firstWord = 0;
		for (index_t j = 0; j < len; ++j)firstWord |= p[j];
		firstWord = ~firstWord;
		for (index_t i = startIndex; i < startIndex + length; ++i)
		{
			if ((pd[i] & firstWord) == 0)
				for (index_t j = 0; j < len; ++j)
				{
					if (p[j] == pd[i])
						return i;
				}
		}
		return -1;
	}
	index_t String::IndexOfAny(PWCharArray wArray, index_t startIndex)const
	{
		return IndexOfAny(wArray, startIndex, -1);
	}

	String& String::Insert(index_t startIndex, const String& xString)//返回在startIndex位置插入xString的新字符串，该字符串不变
	{
		if (type != Normal)Throw<PException >(L"引用型字符串不能进行插入操作");
		if (startIndex<0 || startIndex>Length)Throw<PIndexOutOfRangeException>();
		index_t len = xString.Length;
		if ((index_t)MaxSize > Length + len)
		{
			if (&xString == this)
			{
				Memory::Move(wString + startIndex, wString, Length*sizeof(WCHAR));
				Memory::Copy(wString + startIndex + Length, wString + startIndex + startIndex, (Length - startIndex + 1)*sizeof(WCHAR));
			}
			else
			{
				Memory::Move(wString + len + startIndex, wString + startIndex, (Length - startIndex + 1)*sizeof(WCHAR));
				Memory::Copy(wString + startIndex, xString.wString, len*sizeof(WCHAR));
			}
			Length += len;
		}
		else
		{
			size_t mSize;
			LPWSTR p = String::Strins(ToUnicode(), xString.ToUnicode(), startIndex, Length, len, &mSize);
			len += Length;
			Clear();
			wString = p;
			Length = len;
			const_cast<size_t&>(MaxSize) = mSize;
			type = Normal;
		}
		return *this;
	}

	index_t String::LastIndexOf(WCHAR wc)const//查找指定 Unicode 字符在此字符串中的最后一个匹配项的索引。
	{
		LPCWSTR tmp = wcsrchr(wString, wc);
		if (tmp == NULL)
			return -1;
		return (index_t)(tmp - wString);
	}
	index_t String::LastIndexOf(WCHAR wc, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			LPCWSTR tmp = ToUnicode();
			wc &= 0xcf;
			for (index_t i = Length - 1; i >= startIndex; --i)
			{
				if (tmp[i] == wc || tmp[i] == wc + 32)return i;
			}
			return -1;
		}
		LPCWSTR tmp = wcsrchr(ToUnicode() + startIndex, wc);
		if (tmp == NULL)
			return -1;
		return (index_t)(tmp - ToUnicode());
	}
	index_t String::LastIndexOf(WCHAR wc, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		if (length < 0)length = Length;
		else length += startIndex;
		LPCWSTR tmp = ToUnicode();
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			wc &= 0xcf;
			for (index_t i = length - 1; i >= startIndex; --i)
			{
				if (tmp[i] == wc || tmp[i] == wc + 32)return i;
			}
		}
		else
			for (index_t i = length - 1; i >= startIndex; --i)
			{
				if (tmp[i] == wc)return i;
			}
		return -1;
	}
	index_t String::LastIndexOf(const String& xString)const//查找指定 字符串在此字符串中的最后一个匹配项的索引。
	{
		return String::Last(*this, 0, xString, 0, Length, xString.Length, false);
	}
	index_t String::LastIndexOf(const String& xString, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (startIndex > Length)Throw<PIndexOutOfRangeException>();//参数无效
		return String::Last(*this, startIndex, xString, 0, Length, xString.Length, ignoreCase);
	}
	index_t String::LastIndexOf(const String& xString, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (Length<startIndex + length || startIndex>Length)Throw<PIndexOutOfRangeException>();//参数无效
		if (length < 0)return String::Last(*this, startIndex, xString, 0, Length, xString.Length, ignoreCase);
		return String::Last(*this, startIndex, xString, 0, length + startIndex, xString.Length, ignoreCase);
	}
	//寻找wArray数组中任意字符在此字符串最后出现的位置（不包含结尾字符）
	index_t String::LastIndexOfAny(PWCharArray wArray, index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		if (length < 0)length = Length - startIndex;
		if (!length)return -1;
		const wchar_t* p = wArray;
		const wchar_t* pd = ToUnicode();
		index_t len = wArray->size();
		WCHAR firstWord = 0;
		for (index_t j = 0; j < len; ++j)firstWord |= p[j];
		firstWord = ~firstWord;
		for (index_t i = startIndex + length - 1; i >= startIndex; --i)
		{
			if ((pd[i] & firstWord) == 0)
				for (index_t j = 0; j < len; ++j)
				{
					if (p[j] == pd[i])
						return i;
				}
		}
		return -1;
	}
	index_t String::LastIndexOfAny(PWCharArray wArray, index_t startIndex)const
	{
		return LastIndexOfAny(wArray, startIndex, -1);
	}


	String& String::PadLeft(index_t totalWidth, WCHAR wc)//在左边插入一定数量的wc，使字符串右对齐,返回该字符串
	{
		if (type != Normal)Throw<PException >(L"引用型字符串不能进行插入操作");
		if (totalWidth < 0)Throw<PInvalidArgumentException>();//参数无效
		if ((totalWidth -= Length) <= 0)return *this;
		return *this = String(wc, totalWidth) + *this;
	}
	String& String::PadRight(index_t totalWidth, WCHAR wc)//在右边插入一定数量的wc，使字符串左对齐，
	{
		if (type != Normal)Throw<PException >(L"引用型字符串不能进行插入操作");
		if (totalWidth < 0)Throw<PInvalidArgumentException>();//参数无效
		if ((totalWidth -= Length) <= 0)return *this;
		return *this += String(wc, totalWidth);
	}

	String& String::Remove(index_t startIndex, index_t length)//从startIndex起移除一定长度的内容，length小于0则表示移除后面所有的字符串
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串引用不能移除内容");
		if (startIndex < 0 || length < 0)Throw<PInvalidArgumentException>();
		if (startIndex + length > Length)Throw<PIndexOutOfRangeException>();
		if (length != 0)
		{
			Memory::Move(wString + startIndex, wString + startIndex + length, length*sizeof(WCHAR));
			Length -= length;
			wString[Length] = 0;
			delete[]ChangeAnsi(NULL);
		}
		return *this;
	}
	String& String::Replace(WCHAR wc1, WCHAR wc2, bool ignoreCase)//返回一个新字符串，内容为原字符串中的wc1全替换为wc2
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串引用不能修改内容");
		if (Length == 0)return *this;
		LPCWSTR ws = wString;
		if (ignoreCase && ((wc1 >= L'A'&&wc1 <= L'Z') || (wc1 >= 'a' && wc1 <= 'z')))
		{
			wc1 &= 0xcf;
			for (index_t i = 0; i < Length; ++i)
			{
				if (ws[i] == wc1 || ws[i] == wc1 + 32)wString[i] = wc2;
				else wString[i] = ws[i];
			}
		}
		else for (index_t i = 0; i < Length; ++i)
		{
			if (ws[i] == wc1)wString[i] = wc2;
			else wString[i] = ws[i];
		}
		delete[]ChangeAnsi(NULL);
		return *this;
	}
	String& String::Replace(const String& xString1, const String& xString2, bool ignoreCase)//返回一个新字符串，内容为原字符串中的xString1全替换为xString2
	{
		if (type != Normal)Throw<PException >(L"引用或只读字符串引用不能按字符串替换内容");
		index_t len = Length, count = 0;//len表示该字符串长度，count表示替换次数
		if (!xString1.Length)Throw<PException >(L"指定的被替换字符串为空");
		if (len < xString1.Length)return *this;
		LPCWSTR ws = ToUnicode();
		index_t len1 = xString1.Length;
		index_t len2 = xString2.Length;
		LPCWSTR ws1 = xString1.ToUnicode();
		LPCWSTR ws2 = xString2.ToUnicode();
		for (index_t i = 0; i + len1 <= len;)
		{
			if (!(ignoreCase ? &String::memicmp : &String::memcmp)(ws + i, ws1, len1))
			{
				++count;
				i += len1;
				continue;
			}
			++i;
		}
		count = len + count*(len2 - len1);
		size_t mSize = Memory::GetResize((count + 1)*sizeof(wchar_t)) / sizeof(wchar_t);
		LPWSTR tmp = Memory::New<WCHAR>(mSize);
		index_t t = 0, tt = 0;
		for (index_t i = 0; i + len1 <= len;)
		{
			if (!(ignoreCase ? &String::memicmp : &String::memcmp)(ws + i, ws1, len1))
			{
				if (i > t)Memory::Copy(tmp + tt, ws + t, (i - t)*sizeof(wchar_t));
				tt += i - t;
				if (len2)Memory::Copy(tmp + tt, ws2, len2*sizeof(wchar_t));
				tt += len2;
				i += len1;
				t = i;
				continue;
			}
			++i;
		}
		if (len > t)Memory::Copy(tmp + tt, ws + t, (len - t)*sizeof(wchar_t));
		tmp[count] = 0;
		Clear();
		Length = count;
		const_cast<size_t&>(MaxSize) = mSize;
		wString = tmp;
		return *this;
	}
	String& String::Reverse()
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读引用字符串不能进行反向操作");
		for (index_t i = 0; i + i < Length; ++i)
		{
			Swap(wString[i], wString[Length - i - 1]);
		}
		if (mString) { mString = NULL; }
		return *this;
	}
	String& String::SelfUpper()//返回一个新字符串，新字符串为该所有的小写字母都变成大写
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读引用字符串不能进行大小写转换操作");
		wcsupr(wString, Length);
		return *this;
	}
	String& String::SelfLower()//返回一个新字符串，新字符串为该字符串所有的大写字母都变成小写
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读引用字符串不能进行大小写转换操作");
		wcslwr(wString, Length);
		return *this;
	}
	String String::ToUpper()//返回一个新字符串，新字符串为该所有的小写字母都变成大写
	{
		size_t mSize;
		LPWSTR p = Strcpy(wString, Length, &mSize);
		wcsupr(p, Length);
		return String(Length, p, NULL, mSize);
	}
	String String::ToLower()//返回一个新字符串，新字符串为该字符串所有的大写字母都变成小写
	{
		size_t mSize;
		LPWSTR p = Strcpy(wString, Length, &mSize);
		wcslwr(wString, Length);
		return String(Length, p, NULL, mSize);
	}
	//获取字符串中的英文单词（数字不算，比如“G2 VS2010”得到：“G”“VS”）
	PStringArray String::GetWords()const
	{
		PStringArray strs;
		LPCWSTR pStart = ToUnicode();
		LPCWSTR pEnd = pStart + Length;
		LPWSTR p = const_cast<LPWSTR>(pStart);
		LPCWSTR start = pStart;
		bool alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z');
		while (++p < pEnd)
		{
			if (alpha)
			{
				if ((alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z')) != 0)continue;
				strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
				continue;
			}
			if (!(alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z')))continue;
			start = p;
		}
		if (alpha)strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
		return strs;
	}
	//获取字符串中的数字，比如“G2 VS2010”得到：“2010”
	PStringArray String::GetNumbers()const
	{
		PStringArray strs;
		LPCWSTR pStart = ToUnicode();
		LPCWSTR pEnd = pStart + Length;
		LPWSTR p = const_cast<LPWSTR>(pStart);
		LPCWSTR start = pStart;
		bool alpha = *p >= L'0' && *p <= L'9';
		while (++p < pEnd)
		{
			if (alpha)
			{
				if ((alpha = (*p >= L'0' && *p <= L'9')) != 0)continue;
				strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
				continue;
			}
			if (!(alpha = *p >= L'0' && *p <= L'9'))continue;
			start = p;
		}
		if (alpha)strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
		return strs;
	}
	//获取字符串中的英文和数字（英文和数字可连在一起，比如“G2 VS2010”得到：“G2”、“VS2010”）
	PStringArray String::GetWordsAndNumbers()const
	{
		PStringArray strs;
		LPCWSTR pStart = ToUnicode();
		LPCWSTR pEnd = pStart + Length;
		LPWSTR p = const_cast<LPWSTR>(pStart);
		LPCWSTR start = pStart;
		bool alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z') || (*p >= L'0' && *p <= L'9');
		while (++p < pEnd)
		{
			if (alpha)
			{
				if ((alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z') || (*p >= L'0' && *p <= L'9')) != 0)continue;
				strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
				continue;
			}
			if (!(alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z') || (*p >= L'0' && *p <= L'9')))continue;
			start = p;
		}
		if (alpha)strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
		return strs;
	}
	//返回该字符串的数组表示（不包含结尾字符'\0'）
	PWCharArray String::ToWCharArray(index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		if (length < 0)length = Length - startIndex;
		if (!length)return PWCharArray((index_t)0);
		PWCharArray tmp(length);
		Memory::Copy((WCHAR*)tmp, ToUnicode() + startIndex, sizeof(WCHAR)*length);
		return tmp;
	}
	PString String::Substring(index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (Length < startIndex || Length < startIndex + length)Throw<PIndexOutOfRangeException>();//越界
		if (length < 0)length = Length - startIndex;
		if (length <= 0)return L"";
		if (length == Length)return Clone();
		return new String(length, String::Strcpy(ToUnicode() + startIndex, length), NULL);
	}
	bool String::Equals(const PObject& object)const
	{
		return !Strcmp(wString, object->ToString());
	}
	bool String::Equals(const PString& object)const
	{
		return ToString() == object;
	}

#ifdef _CLR //托管支持
	String::String(System::String^string, index_t count)//创建由count个字符string串接而成的字符串
		:wString(NULL), mString(NULL), Length(0), MaxSize(0), type(Normal)
	{
		if (count < 0)Throw<PInvalidArgumentException>();//参数无效
		if (string)
		{
			System::IntPtr p = System::Runtime::InteropServices::Marshal::StringToHGlobalUni((System::String^)string);
			LPCWSTR ws = (LPCWSTR)p.ToPointer();
			index_t len = ((System::String^)string)->Length;//单个字符串长度
			if (len + 1)
			{
				ResizeAndDestroy(len*count);
				if (len)
					while (count--)LK::Memory::Copy(wString + len*count, ws, len + len);
				wString[Length] = 0;
			}
			System::Runtime::InteropServices::Marshal::FreeHGlobal(p);
		}
	}
	String& String::operator=(System::String^string)
	{
		if (type == ReadOnlyReference)Throw<PException >(L"只读字符串不允许进行赋值操作");
		if (type == Reference)Throw<PException >(L"引用字符串不允许进行赋值操作");
		if (string)
		{
			delete[]ChangeAnsi(NULL);
			System::IntPtr p = System::Runtime::InteropServices::Marshal::StringToHGlobalUni((System::String^)string);
			LPCWSTR ws = (LPCWSTR)p.ToPointer();
			ResizeAndDestroy(((System::String^)string)->Length);
			LK::Memory::Copy(wString, ws, Length*sizeof(WCHAR));
			wString[Length] = 0;
			System::Runtime::InteropServices::Marshal::FreeHGlobal(p);
			return *this;
		}
		return RThrow<String&,PPointerIsNullException >();
	}
	String operator+(const String& xString, System::Object^ obj)
	{
		return xString + String(obj->ToString());
	}
	String operator+(System::Object^ obj, const String& xString)
	{
		return String(obj->ToString()) + xString;
	}
	String operator+(const String& xString, volatile const System::String^ string)
	{
		return xString + String((System::String ^)string);
	}
	String operator+(volatile const System::String^ string, const String& xString)
	{
		return String((System::String ^)string) + xString;
	}
#endif



	//ptr<String>
	ptr<String> ptr<String>::GetEmpty()
	{
		static ptr<String>*p;
		if (p)return *p;
		SpinMutex::GetDefaultRef().Lock();
		if (!p){
			static ptr<String>str = new String();
			str.UseCount.SetEnableThreadSafe(true);
			p = &str;
		}
		SpinMutex::GetDefaultRef().UnLock();
		return *p;
	}
	void ptr<String>::SelfSetChar(index_t index, WCHAR wc)
	{
		String*p = Check();
		if (p->UseCount == 1 || p->type == ReadOnlyReference)
			*this = p->Clone();
		p->SetChar(index, wc);
	}

	index_t ptr<String>::Compare(ptr<String> xString1, ptr<String> xString2, bool ignoreCase)//比较两个字符串的大小,指定是否考虑大小写
	{
		if (xString1.GetPtr() == xString2.GetPtr())return 0;
		if (xString1==null)return -1;
		if (xString2==null)return 1;
		if (ignoreCase)return String::wcsicmp(xString1.Check()->ToUnicode(), xString2.Check()->ToUnicode());
		return String::Strcmp(xString1.Check()->ToUnicode(), xString2.Check()->ToUnicode());
	}
	index_t ptr<String>::Compare(ptr<String> xString1, index_t index1, ptr<String> xString2, index_t index2, index_t length, bool ignoreCase)//比较两个字符串的大小，指定是否忽略大小写
	{
		if (index1 < 0 || index2 < 0)Throw< PInvalidArgumentException>();//参数无效
		if (ignoreCase)return String::wcsnicmp(xString1.Check()->ToUnicode() + index1, xString2.Check()->ToUnicode() + index2, length);
		return String::Strcmp(xString1.Check()->ToUnicode() + index1, xString2.Check()->ToUnicode() + index2, length);
	}
	ptr<String> ptr<String>::Concat(PStringArray  strings)
	{
		index_t len = 0;
		for (index_t i = 0; i < (index_t)strings->size(); ++i)len += strings[i]->Length;
		size_t mSize = Memory::GetResize((len + 1)*sizeof(wchar_t)) / sizeof(wchar_t);
		LPWSTR p = Memory::New<WCHAR>(mSize);
		len = 0;
		for (index_t i = 0; i < (index_t)strings->size(); ++i)
		{
			if (strings[i]->Length)Memory::Copy(p + len, strings[i].ToUnicode(), strings[i]->Length*sizeof(wchar_t));
			len += strings[i]->Length;
		}
		p[len] = 0;
		return ptr<String>(new String(len, p, NULL, mSize));
	}
	ptr<String> ptr<String>::Join(ptr<String> str, PStringArray  strs, index_t startIndex, index_t count)
	{
		if (startIndex < 0)Throw< PInvalidArgumentException>();//参数无效
		if (startIndex > strs->size())Throw< PIndexOutOfRangeException>();
		if (!strs->size())return ptr<String>();
		if (count < 0)count = strs->size() - startIndex;
		index_t len = (count - 1)*str->Length;
		for (index_t i = startIndex; i < startIndex + count; ++i)len += strs[i]->Length;
		size_t mSize = Memory::GetResize((len + 1)*sizeof(wchar_t)) / sizeof(wchar_t);
		LPWSTR p = Memory::New<WCHAR>(mSize);
		len = 0;
		for (index_t i = startIndex; i + 1 < (index_t)startIndex + count; ++i)
		{
			if (strs[i]->Length)Memory::Copy(p + len, strs[i].ToUnicode(), strs[i]->Length*sizeof(wchar_t));
			len += strs[i]->Length;
			if (str->Length)Memory::Copy(p + len, str.ToUnicode(), str->Length*sizeof(wchar_t));
			len += str->Length;
		}
		if (strs[startIndex + count - 1]->Length)Memory::Copy(p + len, strs[startIndex + count - 1].ToUnicode(), strs[startIndex + count - 1]->Length*sizeof(wchar_t));
		len += strs[startIndex + count - 1]->Length;
		p[len] = 0;
		return ptr<String>(new String(len, p, NULL, mSize));
	}

	//由String创建
	ptr<String>::ptr(const String& vs, index_t count) : ptr<Object>(new String(vs, count)) {   }
	//由数字创建字符串
	ptr<String>::ptr(float number, int format) : ptr<Object>(new String(number, (size_t)format)) {   }
	//由数字创建字符串
	ptr<String>::ptr(double number, int format) : ptr<Object>(new String(number, (size_t)format)) {   }
	//由数字创建字符串
	ptr<String>::ptr(unsigned long long number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(unsigned long number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(unsigned int number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(long long number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(long number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(int number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(unsigned short number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(short number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(unsigned char number) : ptr<Object>(new String(number)) {   }
	//由数字创建字符串
	ptr<String>::ptr(signed char number) : ptr<Object>(new String(number)) {   }
	//创建由count个字符c串接而成的字符串
	ptr<String>::ptr(CHAR c, index_t count) : ptr<Object>(new String(c, count)) {   }
	//创建由count个Unicode字符wc串接而成的字符串
	ptr<String>::ptr(WCHAR wc, index_t count) : ptr<Object>(new String(wc, count)) {   }
	//创建由count个wString字符串串接而成的字符串
	ptr<String>::ptr(LPCWSTR wString, index_t count) : ptr<Object>(new String(wString, count)) {   }
	//创建由count个mString字符串串接而成的字符串
	ptr<String>::ptr(LPCSTR mString, index_t count) : ptr<Object>(new String(mString, count)) {   }
	//创建由count个wString字符串串接而成的字符串
	ptr<String>::ptr(PWCharArray wString, index_t count) : ptr<Object>(new String(wString, count)) {   }
	//创建由count个mString字符串串接而成的字符串
	ptr<String>::ptr(PCharArray mString, index_t count) : ptr<Object>(new String(mString, count)) {   }
	ptr<String>::ptr(LPCWSTR wString, index_t startIndex, index_t length, size_t type)//创建由wString字符串startindex字符起数length个字符的字符串，如果length<=-2，则表示到'\0'表示的结尾
	{
		index_t len;
		size_t mSize;
		LPWSTR p;
		if (length < 0)
		{
			p = String::Strcpy(wString + startIndex, &len, &mSize);
			*this = new String(len, p, NULL, mSize);
		}
		else
		{
			if (type == Normal)
			{
				p = String::Strcpy(wString + startIndex, length, &mSize);
				*this = new String(length, p, NULL, mSize);
			}
			else
			{
				*this = new String(length, (LPWSTR)wString, NULL, 0, type);
			}
		}
	}
	ptr<String>::ptr(LPCSTR mString, index_t startIndex, index_t length)//创建由mString字符串startindex字符起数length个字符的字符串，如果length<=-2，则表示到'\0'表示的结尾
		: ptr<Object>(new String(0, String::AnsiToUnicode(mString + startIndex, &length, length), NULL))//引用计数自动加1
	{
		const_cast<index_t&>(Check()->Length) = length;
	}

	//创建由count个xString字符串串接而成的字符串
	ptr<String>::ptr(ptr<String> xString, index_t count)//引用计数自动加1
	{
		if (1 == count)*this = xString.Check();
		else *this = GetEmpty().Check();
		if (count < 0)Throw< PInvalidArgumentException>();//参数无效
		if (count > 1)
		{
			size_t mSize;
			String*p1 = xString.Check();
			LPWSTR p = String::StrRepeat(p1->wString, p1->Length, count, &mSize);
			*this = new String(p1->Length*count, p, NULL, mSize);
		}

	}
	//创建由两个xString串接而成的字符串
	ptr<String>::ptr(ptr<String> xs1, ptr<String> xs2)
	{
		size_t mSize;
		String*p1 = xs1.Check();
		String*p2 = xs2.Check();
		LPWSTR p = String::Strcat(p1->wString, p2->wString, p1->Length, p2->Length, &mSize);
		*this = new String(p1->Length + p2->Length, p, NULL, mSize);
	}
	//创建由三个xString串接而成的字符串
	ptr<String>::ptr(ptr<String> xs1, ptr<String> xs2, ptr<String> xs3)
	{
		size_t mSize;
		String*p1 = xs1.Check();
		String*p2 = xs2.Check();
		String*p3 = xs3.Check();
		LPWSTR p = String::Strcat(p1->wString, p2->wString, p3->wString, p1->Length, p2->Length, p3->Length, &mSize);
		*this = new String(p1->Length + p2->Length + p3->Length, p, NULL, mSize);
	}
	//创建由四个xString串接而成的字符串
	ptr<String>::ptr(ptr<String> xs1, ptr<String> xs2, ptr<String> xs3, ptr<String> xs4)
	{
		size_t mSize;
		String*p1 = xs1.Check();
		String*p2 = xs2.Check();
		String*p3 = xs3.Check();
		String*p4 = xs4.Check();
		LPWSTR p = String::Strcat(p1->wString, p2->wString, p3->wString, p4->wString, p1->Length, p2->Length, p3->Length, p4->Length, &mSize);
		*this = new String(p1->Length + p2->Length + p3->Length + p4->Length, p, NULL, mSize);
	}
	//创建由xStrings数组串接而成的字符串
	ptr<String>::ptr(PStringArray xStrings)
	{
		PStringArray  xa(xStrings);
		if (xa->size() < 0)Throw< PInvalidArgumentException>();//参数无效
		else if (xa->size())
		{
			index_t length = 0;
			for (index_t i = 0; i < xa->size(); ++i)length += xa[i]->Length;
			size_t mSize = Memory::GetResize((length + 1)*sizeof(wchar_t)) / sizeof(wchar_t);
			LPWSTR p = Memory::New<WCHAR>(mSize);
			p[length] = 0;
			length = 0;
			for (index_t i = 0; i < xa->size(); ++i)
			{
				Memory::Copy(p + length, xa[i].ToUnicode(), sizeof(wchar_t)*xa[i]->Length);
				length += xa[i]->Length;
			}
			*this = new String(length, p, NULL, mSize);
		}

	}
	WCHAR ptr<String>::operator[](index_t index)const
	{
		if (*this==null)Throw< PPointerIsNullException >();
		if (index >= 0 && index <= size())
		{
			return Check()->ToUnicode()[index];
		}
		//抛出异常
		Throw< PInvalidArgumentException>();//参数无效
		return 0;
	}
	Collection::ArrayIterator< WCHAR const> ptr<String>::begin()const { return First(); }
	Collection::ArrayIterator< WCHAR const> ptr<String>::end()const { return End(); }
	Collection::ArrayIterator< WCHAR const> ptr<String>::First()const { return Collection::ArrayIterator< WCHAR const>(ToUnicode()); }
	Collection::ArrayIterator< WCHAR const> ptr<String>::Last()const { return Collection::ArrayIterator< WCHAR const>(ToUnicode() ? ToUnicode() + size() - 1 : NULL); }
	Collection::ArrayIterator< WCHAR const> ptr<String>::End()const { return Collection::ArrayIterator< WCHAR const>(ToUnicode() + size()); }
	index_t ptr<String>::size()const
	{
		return Check()->Length;
	}

	ptr<String> ptr<String>::Reverse()const
	{
		return Check()->ToString().SelfReverse();
	}
	ptr<String>& ptr<String>::SelfReverse()
	{
		String*ps = Check();
		if (ps->UseCount == 1 && ps->type != ReadOnlyReference)*this = new String(*ps);
		GetPtr()->Reverse();
		return *this;
	}
	LPCSTR ptr<String>::ToAnsi()const//转换成原始的多字节字符串，const型
	{
		return Check()->ToAnsi();
	}
	LPCWSTR ptr<String>::ToUnicode()const//输出Unicode字符串，const型
	{
		return Check()->ToUnicode();
	}
	unsigned long long ptr<String>::ToUInt64()const
	{
		return Check()->ToUInt64();
	}
	long long ptr<String>::ToInt64()const
	{
		return Check()->ToInt64();
	}
	double ptr<String>::ToDouble()const
	{
		return Check()->ToDouble();
	}
	unsigned long long ptr<String>::ChsToUInt64()const
	{
		return Check()->ChsToUInt64();
	}
	long long ptr<String>::ChsToInt64()const
	{
		return Check()->ChsToInt64();
	}

	ptr<String>::operator LPCSTR()const
	{
		return ToAnsi();
	}
	ptr<String>::operator LPCWSTR()const
	{
		return ToUnicode();
	}

	ptr<String>& ptr<String>::operator=(LPCSTR mString)
	{
		*this = ptr<String>(mString);
		return *this;
	}
	ptr<String>& ptr<String>::operator=(LPCWSTR wString)
	{
		*this = ptr<String>(wString);
		return *this;
	}
	ptr<String>& ptr<String>::operator+=(ptr<String> xString)//串接运算
	{
		String* ps = Check();
		if (ps->UseCount == 1 && ps->type == Normal)
		{
			*ps += *xString.Check();
			return *this;
		}
		return *this = ptr<String>(*this, xString);
	}

	bool operator==(ptr<String> xString1, ptr<String> xString2)//字符串比较
	{
		String* ps1 = xString1.GetPtr();
		String* ps2 = xString2.GetPtr();
		return ps1 == ps2 || (ps1&&ps2&&ps1->Equals(*ps2));
	}
	bool operator!=(ptr<String> xString1, ptr<String> xString2)
	{
		String* ps1 = xString1.GetPtr();
		String* ps2 = xString2.GetPtr();
		return ps1 != ps2 && (!ps1 || !ps2 || !ps1->Equals(*ps2));
	}
	bool operator>(ptr<String> xString1, ptr<String> xString2)
	{
		return *xString1.Check() > *xString2.Check();
	}
	bool operator<(ptr<String> xString1, ptr<String> xString2)
	{
		return *xString1.Check() < *xString2.Check();
	}
	bool operator>=(ptr<String> xString1, ptr<String> xString2)
	{
		return *xString1.Check() >= *xString2.Check();
	}
	bool operator<=(ptr<String> xString1, ptr<String> xString2)
	{
		return *xString1.Check() <= *xString2.Check();
	}
	bool operator==(ptr<String> xString1, LPCWSTR xString2) { return String::Strcmp(xString1, xString2) == 0; }
	bool operator==(LPCWSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) == 0; }
	bool operator==(ptr<String> xString1, LPCSTR xString2) { return String::Strcmp(xString1, xString2) == 0; }
	bool operator==(LPCSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) == 0; }
	bool operator!=(ptr<String> xString1, LPCWSTR xString2) { return String::Strcmp(xString1, xString2) != 0; }
	bool operator!=(LPCWSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) != 0; }
	bool operator!=(ptr<String> xString1, LPCSTR xString2) { return String::Strcmp(xString1, xString2) != 0; }
	bool operator!=(LPCSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) != 0; }
	bool operator>(ptr<String> xString1, LPCWSTR xString2) { return String::Strcmp(xString1, xString2) > 0; }
	bool operator>(LPCWSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) > 0; }
	bool operator>(ptr<String> xString1, LPCSTR xString2) { return String::Strcmp(xString1, xString2) > 0; }
	bool operator>(LPCSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) > 0; }
	bool operator<(ptr<String> xString1, LPCWSTR xString2) { return String::Strcmp(xString1, xString2) < 0; }
	bool operator<(LPCWSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) < 0; }
	bool operator<(ptr<String> xString1, LPCSTR xString2) { return String::Strcmp(xString1, xString2) < 0; }
	bool operator<(LPCSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) < 0; }
	bool operator>=(ptr<String> xString1, LPCWSTR xString2) { return String::Strcmp(xString1, xString2) >= 0; }
	bool operator>=(LPCWSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) >= 0; }
	bool operator>=(ptr<String> xString1, LPCSTR xString2) { return String::Strcmp(xString1, xString2) >= 0; }
	bool operator>=(LPCSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) >= 0; }
	bool operator<=(ptr<String> xString1, LPCWSTR xString2) { return String::Strcmp(xString1, xString2) <= 0; }
	bool operator<=(LPCWSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) <= 0; }
	bool operator<=(ptr<String> xString1, LPCSTR xString2) { return String::Strcmp(xString1, xString2) <= 0; }
	bool operator<=(LPCSTR xString1, ptr<String> xString2) { return String::Strcmp(xString1, xString2) <= 0; }

	ptr<String> operator+(ptr<String> xString1, ptr<String> xString2)
	{
		return ptr<String>(xString1, xString2);
	}

	ptr<String> operator+(ptr<String> xString1, LPCWSTR xString2)
	{
		return xString1 + ptr<String>(xString2);
	}
	ptr<String> operator+(LPCWSTR xString1, ptr<String> xString2)
	{
		return ptr<String>(xString1) + xString2;
	}
	ptr<String> operator+(ptr<String> xString1, LPCSTR xString2)
	{
		return xString1 + ptr<String>(xString2);
	}
	ptr<String> operator+(LPCSTR xString1, ptr<String> xString2)
	{
		return ptr<String>(xString1) + xString2;
	}

	index_t ptr<String>::CompareTo(ptr<String> string)const
	{
		return Compare(*this, string);
	}
	//复制到多字节数组，返回复制的字节数，结尾的'\0'算一个字节
	index_t ptr<String>::CopyTo(PCharArray destination, index_t sourceByteIndex, index_t destinationByteIndex, index_t lengthOfByte)const
	{
		index_t maxlen = String::Strlen(ToAnsi());
		if (maxlen < 0)return 0;
		if (sourceByteIndex<0 || destinationByteIndex < 0)Throw< PInvalidArgumentException>();//参数无效
		if (sourceByteIndex > maxlen || destinationByteIndex>destination->size())Throw< PIndexOutOfRangeException>();
		if (lengthOfByte < 0)
		{
			lengthOfByte = destination->size() - destinationByteIndex <= maxlen - sourceByteIndex ? destination->size() - destinationByteIndex : maxlen + 1 - sourceByteIndex;
		}
		if (!lengthOfByte)return 0;
		Memory::Copy(destination + destinationByteIndex, ToAnsi() + sourceByteIndex, lengthOfByte);
		return lengthOfByte;
	}
	//复制到Unicode数组，返回复制的字符数，结尾的'\0'算一个字符(两个字节)
	index_t ptr<String>::CopyTo(PWCharArray destination, index_t sourceIndex, index_t destinationIndex, index_t length)const
	{
		if (sourceIndex<0 || destinationIndex < 0)Throw< PInvalidArgumentException>();//参数无效
		if (sourceIndex > size() || destinationIndex>destination->size())Throw< PIndexOutOfRangeException>();
		if (length < 0)
		{
			length = (index_t)destination->size() - destinationIndex <= size() - sourceIndex ? destination->size() - destinationIndex : size() + 1 - sourceIndex;
		}
		if (!length)return 0;
		Memory::Copy(destination + destinationIndex, ToUnicode() + sourceIndex, length*sizeof(wchar_t));
		return length;
	}
	bool ptr<String>::Contains(ptr<String> xString)const
	{
		return String::Find(*Check(), 0, *xString.Check(), 0, size(), xString->Length, false) >= 0;
	}
	size_t ptr<String>::CountOf(WCHAR wch)const
	{
		return Check()->CountOf(wch);
	}
	size_t ptr<String>::CountOf(ptr<String> str)const
	{
		return Check()->CountOf(*str.Check());
	}
	bool ptr<String>::EndsWith(ptr<String> xString)const//判断该字符串是否以xString结尾
	{
		index_t len = xString->Length;
		if (size() < len || !len)
			return false;
		return !String::memcmp(ToUnicode() + size() - len, xString.ToUnicode(), len);
	}
	bool ptr<String>::StartsWith(ptr<String> xString)const//判断该字符串是否以xString开头
	{
		if (size() < xString->Length)return false;
		return !String::memcmp(ToUnicode(), xString.ToUnicode(), xString->Length);
	}

	index_t ptr<String>::IndexOf(WCHAR wc)const//查找指定 Unicode 字符在此字符串中的第一个匹配项的索引。
	{
		return String::Strchr(ToUnicode(), wc);
	}
	index_t ptr<String>::IndexOf(WCHAR wc, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex)Throw<PIndexOutOfRangeException>();
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			wc &= 0xcf;
			index_t i = startIndex;
			index_t len = size();
			LPCWSTR p = ToUnicode();
			while (i < len)
			{
				if (p[i] == wc || p[i] == wc + 32)break;
				++i;
			}
			if (i == len)return -1;
			return i;
		}
		index_t tmp = String::Strchr(ToUnicode() + startIndex, wc);
		if (tmp < 0)return tmp;
		return tmp + startIndex;
	}
	index_t ptr<String>::IndexOf(WCHAR wc, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			wc &= 0xcf;
			index_t i = startIndex;
			index_t len = startIndex + length;
			LPCWSTR p = ToUnicode();
			while (i < len)
			{
				if (p[i] == wc || p[i] == wc + 32)break;
				++i;
			}
			if (i == len)return -1;
			return i;
		}
		index_t tmp = String::Strchr(ToUnicode() + startIndex, wc, length);
		if (tmp < 0)return tmp;
		return tmp + startIndex;
	}

	index_t ptr<String>::IndexOf(ptr<String> xString)const//查找指定 字符串在此字符串中的第一个匹配项的索引。
	{
		return String::Find(*Check(), 0, *xString.Check(), 0, size(), xString->Length, false);
	}
	index_t ptr<String>::IndexOf(ptr<String> xString, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (size() < startIndex)Throw<PIndexOutOfRangeException>();//参数无效
		return String::Find(*Check(), startIndex, *xString.Check(), 0, size() - startIndex, xString->Length, ignoreCase);
	}
	index_t ptr<String>::IndexOf(ptr<String> xString, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		return String::Find(*Check(), startIndex, *xString.Check(), 0, length, xString->Length, ignoreCase);
	}
	//寻找wArray数组中任意字符在此字符串首次出现的位置（不包含结尾字符）
	index_t ptr<String>::IndexOfAny(PWCharArray wArray, index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		if (length < 0)length = size() - startIndex;
		if (!length)return -1;
		const wchar_t* p = wArray;
		const wchar_t* pd = ToUnicode();
		index_t len = wArray->size();
		for (index_t i = startIndex; i < startIndex + length; ++i)
		{
			for (index_t j = 0; j < len; ++j)
			{
				if (p[j] == pd[i])
					return i;
			}
		}
		return -1;
	}
	index_t ptr<String>::IndexOfAny(PWCharArray wArray, index_t startIndex)const
	{
		return IndexOfAny(wArray, startIndex, -1);
	}

	ptr<String> ptr<String>::Insert(index_t startIndex, ptr<String> xString)const//返回在startIndex位置插入xString的新字符串，该字符串不变
	{
		if (*this==null || xString==null)Throw<PPointerIsNullException >();
		return ptr<String>(new String(size() + xString->Length, String::Strins(ToUnicode(), xString.ToUnicode(), startIndex, size(), xString->Length), NULL));
	}

	index_t ptr<String>::LastIndexOf(WCHAR wc)const//查找指定 Unicode 字符在此字符串中的最后一个匹配项的索引。
	{
		LPCWSTR tmp = wcsrchr(ToUnicode(), wc);
		if (tmp == NULL)
			return -1;
		return (index_t)(tmp - ToUnicode());
	}
	index_t ptr<String>::LastIndexOf(WCHAR wc, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			LPCWSTR tmp = ToUnicode();
			wc &= 0xcf;
			for (index_t i = size() - 1; i >= startIndex; --i)
			{
				if (tmp[i] == wc || tmp[i] == wc + 32)return i;
			}
			return -1;
		}
		LPCWSTR tmp = wcsrchr(ToUnicode() + startIndex, wc);
		if (tmp == NULL)
			return -1;
		return (index_t)(tmp - ToUnicode());
	}
	index_t ptr<String>::LastIndexOf(WCHAR wc, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		if (length < 0)length = size();
		else length += startIndex;
		LPCWSTR tmp = ToUnicode();
		if (ignoreCase && ((wc >= L'A'&&wc <= L'Z') || (wc >= 'a' && wc <= 'z')))
		{
			wc &= 0xcf;
			for (index_t i = length - 1; i >= startIndex; --i)
			{
				if (tmp[i] == wc || tmp[i] == wc + 32)return i;
			}
		}
		else
			for (index_t i = length - 1; i >= startIndex; --i)
			{
				if (tmp[i] == wc)return i;
			}
		return -1;
	}
	index_t ptr<String>::LastIndexOf(const ptr<String> xString)const//查找指定 字符串在此字符串中的最后一个匹配项的索引。
	{
		return String::Last(*Check(), 0, *xString.Check(), 0, size(), xString->Length, false);
	}
	index_t ptr<String>::LastIndexOf(const ptr<String> xString, index_t startIndex, bool ignoreCase)const//指定起始位置并指定是否忽略大小写
	{
		if (startIndex > size())Throw<PIndexOutOfRangeException>();//参数无效
		return String::Last(*Check(), startIndex, *xString.Check(), 0, size(), xString->Length, ignoreCase);
	}
	index_t ptr<String>::LastIndexOf(const ptr<String> xString, index_t startIndex, index_t length, bool ignoreCase)const
	{
		if (size() < startIndex + length || startIndex > size())Throw<PIndexOutOfRangeException>();//参数无效
		if (length < 0)return String::Last(*Check(), startIndex, *xString.Check(), 0, size(), xString->Length, ignoreCase);
		return String::Last(*Check(), startIndex, *xString.Check(), 0, length + startIndex, xString->Length, ignoreCase);
	}
	//寻找wArray数组中任意字符在此字符串最后出现的位置（不包含结尾字符）
	index_t ptr<String>::LastIndexOfAny(const PWCharArray wArray, index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		if (length < 0)length = size() - startIndex;
		if (!length)return -1;
		const wchar_t* p = wArray;
		const wchar_t* pd = ToUnicode();
		index_t len = wArray->size();
		for (index_t i = startIndex + length - 1; i >= startIndex; --i)
		{
			for (index_t j = 0; j < len; ++j)
			{
				if (p[j] == pd[i])
					return i;
			}
		}
		return -1;
	}
	index_t ptr<String>::LastIndexOfAny(PWCharArray wArray, index_t startIndex)const
	{
		return LastIndexOfAny(wArray, startIndex, -1);
	}


	ptr<String> ptr<String>::PadLeft(index_t totalWidth, WCHAR wc)const//在左边插入一定数量的wc，使字符串右对齐，返回一个有totalWidth个字符的字符串，该字符串不变，如果原字符串的长度大于totalWidth，则直接返回该字符串
	{
		if (totalWidth < 0)Throw<PInvalidArgumentException>();//参数无效
		if ((totalWidth -= size()) <= 0)return *this;
		return ptr<String>(wc, totalWidth) + *this;
	}
	ptr<String> ptr<String>::PadRight(index_t totalWidth, WCHAR wc)const//在右边插入一定数量的wc，使字符串左对齐，
	{
		if (totalWidth < 0)Throw<PInvalidArgumentException>();//参数无效
		if ((totalWidth -= size()) <= 0)return *this;
		return *this + ptr<String>(wc, totalWidth);
	}

	ptr<String> ptr<String>::Remove(index_t startIndex, index_t len)const//从startIndex起移除一定长度的内容，返回得到的新字符串，length小于0则表示移除后面所有的字符串
	{
		if (len == size())return GetEmpty();
		if (len == 0)return *this;
		if (startIndex < 0 || len < 0)Throw<PInvalidArgumentException>();
		if (size() < startIndex || startIndex + len > size())Throw<PIndexOutOfRangeException>();
		LPWSTR p = new WCHAR[size() - len + 1];
		p[size() - len] = 0;
		Memory::Copy(p, ToUnicode(), startIndex*sizeof(wchar_t));
		Memory::Copy(p + startIndex, ToUnicode() + startIndex + len, (size() - len - startIndex)*sizeof(wchar_t));
		return ptr<String>(new String(size() - len, p, NULL));
	}
	ptr<String> ptr<String>::Replace(WCHAR wc1, WCHAR wc2, bool ignoreCase)const//返回一个新字符串，内容为原字符串中的wc1全替换为wc2
	{
		index_t len;
		if ((len = size()) == 0)return *this;
		LPCWSTR ws = ToUnicode();
		LPWSTR tmp = new WCHAR[len + 1];
		if (ignoreCase && ((wc1 >= L'A'&&wc1 <= L'Z') || (wc1 >= 'a' && wc1 <= 'z')))
		{
			wc1 &= 0xcf;
			for (index_t i = 0; i < len; ++i)
			{
				if (ws[i] == wc1 || ws[i] == wc1 + 32)tmp[i] = wc2;
				else tmp[i] = ws[i];
			}
		}
		else for (index_t i = 0; i < len; ++i)
		{
			if (ws[i] == wc1)tmp[i] = wc2;
			else tmp[i] = ws[i];
		}
		tmp[len] = 0;
		return ptr<String>(new String(len, tmp, NULL));
	}
	ptr<String> ptr<String>::Replace(ptr<String> xString1, ptr<String> xString2, bool ignoreCase)const//返回一个新字符串，内容为原字符串中的xString1全替换为xString2
	{
		index_t len = size(), count = 0;//len表示该字符串长度，count表示替换次数
		if (!xString1->Length)Throw<PInvalidArgumentException>(L"指定的被替换字符串为空");
		if (len < xString1->Length)return *this;
		index_t len1 = xString1->Length;
		index_t len2 = xString2->Length;
		LPWSTR ws = (LPWSTR)ToUnicode();
		LPWSTR ws1 = (LPWSTR)xString1.ToUnicode();
		LPWSTR ws2 = (LPWSTR)xString2.ToUnicode();
		for (index_t i = 0; i + len1 <= len;)
		{
			if (!(ignoreCase ? &String::memicmp : &String::memcmp)(ws + i, ws1, len1))
			{
				++count;
				i += len1;
				continue;
			}
			++i;
		}
		count = len + count*(len2 - len1);
		LPWSTR tmp = new WCHAR[count + 1];
		index_t t = 0, tt = 0;
		for (index_t i = 0; i + len1 <= len;)
		{
			if (!(ignoreCase ? &String::memicmp : &String::memcmp)(ws + i, ws1, len1))
			{
				if (i > t)Memory::Copy(tmp + tt, ws + t, (i - t)*sizeof(wchar_t));
				tt += i - t;
				if (len2)
				{
					Memory::Copy(tmp + tt, ws2, len2*sizeof(wchar_t));
					tt += len2;
				}
				i += len1;
				t = i;
				continue;
			}
			++i;
		}
		if (len > t)Memory::Copy(tmp + tt, ws + t, (len - t)*sizeof(wchar_t));
		tmp[count] = 0;
		return ptr<String>(new String(count, tmp, NULL));
	}

	ptr<String> ptr<String>::ToUpper()const//返回一个新字符串，新字符串为该所有的小写字母都变成大写
	{
		String* ps = Check();
		if (ps->UseCount == 1 && ps->type != ReadOnlyReference)
		{
			ps->SelfUpper();
			return *this;
		}
		LPCWSTR tmp;
		if ((tmp = ps->wString) == NULL)return *this;
		index_t length = ps->Length;
		size_t mSize;
		LPWSTR ws = String::Strcpy(tmp, length, &mSize);
		String::wcsupr(ws, length);
		return ptr<String>(new String(length, ws, NULL, mSize));
	}
	ptr<String> ptr<String>::ToLower()const//返回一个新字符串，新字符串为该字符串所有的大写字母都变成小写
	{
		String* ps = Check();
		if (ps->UseCount == 1 && ps->type != ReadOnlyReference)
		{
			ps->SelfLower();
			return *this;
		}
		LPCWSTR tmp;
		if ((tmp = ps->wString) == NULL)return *this;
		index_t length = size();
		size_t mSize;
		LPWSTR ws = String::Strcpy(tmp, length, &mSize);
		String::wcslwr(ws, length);
		return ptr<String>(new String(length, ws, NULL, mSize));
	}
	ptr<String>& ptr<String>::SelfRemove(index_t startIndex, index_t len)//从startIndex起移除一定长度的内容，返回得到的新字符串，length小于0则表示移除后面所有的字符串
	{
		if (len == 0)return *this;
		String*ps = Check();
		if (ps->UseCount == 1 && ps->type != ReadOnlyReference)
		{
			ps->Remove(startIndex, len);
			return *this;
		}
		if (startIndex < 0 || len < 0)Throw<PInvalidArgumentException>();
		if (ps->Length<startIndex || startIndex + len>ps->Length)Throw<PIndexOutOfRangeException>();
		if (len == ps->Length)return *this = GetEmpty();
		LPWSTR p = new WCHAR[ps->Length - len + 1];
		p[ps->Length - len] = 0;
		Memory::Copy(p, ps->wString, startIndex*sizeof(wchar_t));
		Memory::Copy(p + startIndex, ps->wString + startIndex + len, (ps->Length - len - startIndex)*sizeof(wchar_t));
		return *this = new String(ps->Length - len, p, NULL);
	}
	ptr<String>& ptr<String>::SelfReplace(WCHAR wc1, WCHAR wc2, bool ignoreCase)//返回一个新字符串，内容为原字符串中的wc1全替换为wc2
	{
		String*ps = Check();
		if (ps->UseCount == 1 && ps->type != ReadOnlyReference)
		{
			ps->Replace(wc1, wc2, ignoreCase);
			return *this;
		}
		index_t len;
		if ((len = ps->Length) == 0)return *this;
		LPCWSTR ws = ps->wString;
		LPWSTR tmp = new WCHAR[len + 1];
		if (ignoreCase && ((wc1 >= L'A'&&wc1 <= L'Z') || (wc1 >= 'a' && wc1 <= 'z')))
		{
			wc1 &= 0xcf;
			for (index_t i = 0; i < len; ++i)
			{
				if (ws[i] == wc1 || ws[i] == wc1 + 32)tmp[i] = wc2;
				else tmp[i] = ws[i];
			}
		}
		else for (index_t i = 0; i < len; ++i)
		{
			if (ws[i] == wc1)tmp[i] = wc2;
			else tmp[i] = ws[i];
		}
		tmp[len] = 0;
		return *this = new String(len, tmp, NULL);
	}
	ptr<String>& ptr<String>::SelfReplace(ptr<String> xString1, ptr<String> xString2, bool ignoreCase)//返回一个新字符串，内容为原字符串中的xString1全替换为xString2
	{
		String* p = Check();
		String* p1 = xString1.Check();
		String* p2 = xString2.Check();
		if (p->UseCount == 1 && p->type == Normal)
		{
			p->Replace(*p1, *p2, ignoreCase);
			return *this;
		}
		index_t len = p->Length, count = 0;//len表示该字符串长度，count表示替换次数
		if (!p1->Length)return *this = GetEmpty();
		if (len < p1->Length)return *this;
		LPCWSTR ws = p->wString;
		index_t len1 = p1->Length;
		index_t len2 = p2->Length;
		LPCWSTR ws1 = p1->wString;
		LPCWSTR ws2 = p2->wString;
		for (index_t i = 0; i + len1 <= len;)
		{
			if (!(ignoreCase ? &String::memicmp : &String::memcmp)(ws + i, ws1, len1))
			{
				++count;
				i += len1;
				continue;
			}
			++i;
		}
		count = len + count*(len2 - len1);
		LPWSTR tmp = new WCHAR[count + 1];
		index_t t = 0, tt = 0;
		for (index_t i = 0; i + len1 <= len;)
		{
			if (!(ignoreCase ? &String::memicmp : &String::memcmp)(ws + i, ws1, len1))
			{
				if (i > t)Memory::Copy(tmp + tt, ws + t, (i - t)*sizeof(wchar_t));
				tt += i - t;
				if (len2)Memory::Copy(tmp + tt, ws2, len2*sizeof(wchar_t));
				tt += len2;
				i += len1;
				t = i;
				continue;
			}
			++i;
		}
		if (len > t)Memory::Copy(tmp + tt, ws + t, (len - t)*sizeof(wchar_t));
		tmp[count] = 0;
		return *this = new String(count, tmp, NULL);
	}

	ptr<String>& ptr<String>::SelfToUpper()//返回一个新字符串，新字符串为该所有的小写字母都变成大写
	{
		String* p = Check();
		if (p->UseCount == 1)
		{
			p->ToUpper();
			return *this;
		}
		LPCWSTR tmp;
		if ((tmp = p->wString) == NULL)return *this;
		index_t length = p->Length;
		LPWSTR ws = String::Strcpy(tmp);
		String::wcsupr(ws, length);
		return *this = new String(length, ws, NULL);
	}
	ptr<String>& ptr<String>::SelfToLower()//返回一个新字符串，新字符串为该字符串所有的大写字母都变成小写
	{
		String* p = Check();
		if (p->UseCount == 1)
		{
			p->ToLower();
			return *this;
		}
		LPCWSTR tmp;
		if ((tmp = ToUnicode()) == NULL)return *this;
		index_t length = size();
		LPWSTR ws = String::Strcpy(tmp);
		String::wcslwr(ws, length);
		return *this = new String(length, ws, NULL);
	}
	//获取字符串中的英文单词（数字不算，比如“G2 VS2010”得到：“G”“VS”）
	PStringArray  ptr<String>::GetWords()const
	{
		String* ps = Check();
		PStringArray strs = PStringArray ((index_t)0);
		LPCWSTR pStart = ps->wString;
		LPCWSTR pEnd = pStart + ps->Length;
		LPWSTR p = const_cast<LPWSTR>(pStart);
		LPCWSTR start = pStart;
		bool alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z');
		while (++p < pEnd)
		{
			if (alpha)
			{
				if ((alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z')) != 0)continue;
				strs->Append(Substring((index_t)(start - pStart), (index_t)(p - start)));
				continue;
			}
			if (!(alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z')))continue;
			start = p;
		}
		if (alpha)strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
		return strs;
	}
	//获取字符串中的数字，比如“G2 VS2010”得到：“2010”
	PStringArray  ptr<String>::GetNumbers()const
	{
		String* ps = Check();
		PStringArray strs = PStringArray ((index_t)0);
		LPCWSTR pStart = ps->wString;
		LPCWSTR pEnd = pStart + ps->Length;
		LPWSTR p = const_cast<LPWSTR>(pStart);
		LPCWSTR start = pStart;
		bool alpha = *p >= L'0' && *p <= L'9';
		while (++p < pEnd)
		{
			if (alpha)
			{
				if ((alpha = (*p >= L'0' && *p <= L'9')) != 0)continue;
				strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
				continue;
			}
			if (!(alpha = *p >= L'0' && *p <= L'9'))continue;
			start = p;
		}
		if (alpha)strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
		return strs;
	}
	//获取字符串中的英文和数字（英文和数字可连在一起，比如“G2 VS2010”得到：“G2”、“VS2010”）
	PStringArray  ptr<String>::GetWordsAndNumbers()const
	{
		String* ps = Check();
		PStringArray strs = PStringArray ((index_t)0);
		LPCWSTR pStart = ps->wString;
		LPCWSTR pEnd = pStart + ps->Length;
		LPWSTR p = const_cast<LPWSTR>(pStart);
		LPCWSTR start = pStart;
		bool alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z') || (*p >= L'0' && *p <= L'9');
		while (++p < pEnd)
		{
			if (alpha)
			{
				if ((alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z') || (*p >= L'0' && *p <= L'9')) != 0)continue;
				strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
				continue;
			}
			if (!(alpha = (*p >= L'a' && *p <= L'z') || (*p >= L'A' && *p <= L'Z') || (*p >= L'0' && *p <= L'9')))continue;
			start = p;
		}
		if (alpha)strs->Append(Substring(index_t(start - pStart), index_t(p - start)));
		return strs;
	}
	//返回该字符串的数组表示（不包含结尾字符'\0'）
	PWCharArray ptr<String>::ToWCharArray(index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		if (length < 0)length = size() - startIndex;
		if (!length)return PWCharArray((index_t)0);
		PWCharArray tmp(length);
		Memory::Copy((WCHAR*)tmp, ToUnicode() + startIndex, length*sizeof(WCHAR));
		return tmp;
	}
	ptr<String> ptr<String>::Substring(index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (size() < startIndex || size() < startIndex + length)Throw<PIndexOutOfRangeException>();
		if (length < 0)length = size() - startIndex;
		if (length <= 0)return GetEmpty();
		if (length == size())return *this;
		return ptr<String>(new String(length, String::Strcpy(ToUnicode() + startIndex, length), NULL));
	}
	//检索一个子字符串，当length<0时返回至结尾的子字符串 ,与Substring方法不同，这里的length表示字节长度，而且为了保证字符的完整性，返回的字符串的字节长度有可能小于指定的length。,startIndex<0或startIndex>size()将抛出异常
	ptr<String> ptr<String>::Cut(index_t startIndex, index_t length)const
	{
		if (startIndex < 0)Throw<PInvalidArgumentException>();//参数无效
		if (startIndex > size())Throw<PIndexOutOfRangeException>();
		if (length < 0)return Substring(startIndex, length);
		if (!size())return *this;
		index_t count = 0;
		index_t i;
		LPCWSTR pstr = ToUnicode();
		for (i = startIndex; i < size(); ++i)
		{
			if (pstr[i] == L'\n' || pstr[i] == L'\r')//回车与换行
			{
				if (pstr[i + 1] + pstr[i] == L'\n' + L'\r')
					++i;
				return Substring(startIndex, i + 1 - startIndex);
			}
			if (pstr[i] < 128 && pstr[i]>0)
			{
				++count;
				if (count >= length)
				{
					if (pstr[i + 1] == L'\n' || pstr[i + 1] == L'\r')//回车与换行
					{
						++i;
						if (i < size() && pstr[i + 1] + pstr[i] == L'\n' + L'\r')
							++i;
					}
					return Substring(startIndex, i + 1 - startIndex);
				}
			}
			else
			{
				count += sizeof(wchar_t);
				if (count >= length)
				{
					if (count > length)return Substring(startIndex, i - startIndex);
					if (pstr[i + 1] == L'\n' || pstr[i + 1] == L'\r')//回车与换行
					{
						++i;
						if (i < size() && pstr[i + 1] + pstr[i] == L'\n' + L'\r')
							++i;
					}
					return Substring(startIndex, i + 1 - startIndex);
				}
			}
		}
		return Substring(startIndex, i - startIndex);
	}
	bool ptr<String>::IsNullOrEmpty()const//返回一个值，指使该字符串是否为空（包括NULL和""）
	{
		return *this==null || Check()->Length == 0;
	}
	bool ptr<String>::Equals(ptr<String> object)const
	{
		return !Compare(*this, object);
	}
	//返回使用指定字符做分隔符分割形成的字符串数组,并指定最大数量
	PStringArray  ptr<String>::Split(WCHAR ch, index_t maxCount)const
	{
		if (*this==null)Throw<PPointerIsNullException >();
		if (maxCount < 0)Throw<PInvalidArgumentException>();//参数无效
		if (!size() || maxCount == 0)return PStringArray ((index_t)0);
		index_t len = size();
		LPCWSTR p = ToUnicode();
		index_t count = 0;
		for (index_t i = 0; i < len; ++i)if (p[i] == ch)
		{
			if (count + 1 < maxCount)++count;
			else break;
		}
		ptr<String>* pstrs = new ptr<String>[count + 1];
		index_t lasti = 0;
		count = 0;
		for (index_t i = 0; i < len; ++i)
		{
			if (p[i] == ch)
			{
				pstrs[count] = Substring(lasti, i - lasti);
				lasti = i + 1;
				++count;
				if (count >= maxCount)
					break;
			}
		}
		if (count < maxCount)
		{
			pstrs[count] = Substring(lasti, len - lasti);
			return PStringArray (count + 1, pstrs);
		}
		else
			return PStringArray (count, pstrs);
	}
	//返回使用指定字符做分隔符分割形成的字符串数组
	PStringArray  ptr<String>::Split(WCHAR ch)const
	{
		return Split(ch, MaxValues::Of<index_t>());
	}
	//返回使用指定字符串做分隔符分割形成的字符串数组,并指定最大数量
	PStringArray  ptr<String>::Split(ptr<String> str, index_t maxCount)const
	{
		if (*this==null || str==null)Throw<PPointerIsNullException >();
		if (maxCount < 0)Throw<PInvalidArgumentException>();//参数无效
		if (maxCount == 0 || str.IsNullOrEmpty())return PStringArray ((index_t)0);
		if (size() < str->Length)return  PStringArray (1, *this);
		index_t len = size() - str->Length;
		index_t slen = str->Length;
		LPCWSTR p = ToUnicode();
		LPCWSTR ws = str.ToUnicode();
		index_t count = 0;
		for (index_t i = 0; i <= len;)
			if (!String::memcmp(p + i, ws, slen))
			{
				if (count + 1 < maxCount)++count;
				else break;
				i += slen;
			}
			else ++i;
			PStringArray  strs(count + 1);
			ptr<String>* pstrs = strs;
			index_t lasti = 0;
			count = 0;
			for (index_t i = 0; i <= len;)
			{
				if (!String::memcmp(p + i, ws, slen))
				{
					if (count + 1 >= maxCount)
						break;
					pstrs[count] = Substring(lasti, i - lasti);
					++count;
					i += slen;
					lasti = i;
				}
				else ++i;
			}
			if (count < maxCount)
			{
				pstrs[count] = Substring(lasti, len + slen - lasti);
				strs->Resize(count + 1);
			}
			else strs->Resize(count);
			return strs;
	}
	//返回使用指定字符做分隔符分割形成的字符串数组,并指定最大数量,是否移除空字符串
	PStringArray  ptr<String>::Split(WCHAR ch, index_t maxCount, bool removeEmpty)const
	{
		if (*this==null)Throw<PPointerIsNullException >();
		if (maxCount < 0)Throw<PInvalidArgumentException>();//参数无效
		if (!size() || maxCount == 0)return PStringArray ((index_t)0);
		index_t len = size();
		LPCWSTR p = ToUnicode();
		index_t count = 0;
		index_t lasti = 0;
		for (index_t i = 0; i < len; ++i)
		{
			if (p[i] == ch)
			{
				if (removeEmpty && lasti == i)
				{
					lasti = i + 1;
					continue;
				}
				if (count + 1 < maxCount)++count;
				else break;
				lasti = i + 1;
			}
		}
		PStringArray  strs(count + 1);
		ptr<String>* pstrs = strs;
		count = 0;
		lasti = 0;
		for (index_t i = 0; i < len; ++i)
		{
			if (p[i] == ch)
			{
				if (!removeEmpty || i > lasti)
				{
					pstrs[count] = Substring(lasti, i - lasti);
					++count;
				}
				lasti = i + 1;
				if (count >= maxCount)
					break;
			}
		}
		if (count<maxCount && (!removeEmpty || len>lasti))
		{
			pstrs[count] = Substring(lasti, len - lasti);
			strs->Resize(count + 1);
		}
		else strs->Resize(count);
		return strs;
	}
	//返回使用指定字符串做分隔符分割形成的字符串数组,并指定最大数量,是否移除空字符串
	PStringArray  ptr<String>::Split(ptr<String> str, index_t maxCount, bool removeEmpty)const
	{
		if (*this==null)Throw<PPointerIsNullException >();
		if (maxCount < 0)Throw<PInvalidArgumentException>();//参数无效
		if (!size() || maxCount == 0)return PStringArray ((index_t)0);
		index_t len = size();
		LPCWSTR p = ToUnicode();

		index_t count = 0;//分隔字符串数量（如果removeEmpty为true则连续的分隔符表示1个分隔符）
		index_t lasti = 0;//上一个分隔符末尾+1的位置
		for (index_t i = 0; i < len;)
		{
			if (i + str->Length < len && !String::memcmp(i + p, str.ToUnicode(), str->Length))
			{
				if (removeEmpty && lasti == i)
				{
					i += str->Length;
					lasti = i;
					continue;
				}
				if (count + 1 < maxCount)++count;
				else break;
				i += str->Length;
				lasti = i;
			}
			else ++i;
		}
		PStringArray  strs(count + 1);
		ptr<String>* pstrs = strs;
		count = 0;
		lasti = 0;
		for (index_t i = 0; i < len;)
		{
			if (i + str->Length < len && !String::memcmp(i + p, str.ToUnicode(), str->Length))
			{
				if (!removeEmpty || i > lasti)
				{
					pstrs[count] = Substring(lasti, i - lasti);
					++count;
				}
				i += str->Length;
				lasti = i;
				if (count >= maxCount)
					break;
			}
			else ++i;
		}
		if (count<maxCount && (!removeEmpty || len>lasti))
		{
			pstrs[count] = Substring(lasti, len - lasti);
			strs->Resize(count + 1);
		}
		else strs->Resize(count);
		return strs;
	}
	//返回使用指定字符数组中出现的字符做分隔符分割形成的字符串数组,并指定最大数量,并指明是否移除空字符串
	PStringArray  ptr<String>::Split(PWCharArray chs, index_t maxCount, bool removeEmpty)const
	{
		if (*this==null)Throw<PPointerIsNullException >();
		if (maxCount < 0)Throw<PInvalidArgumentException>();//参数无效
		if (!size() || maxCount == 0)return PStringArray ((index_t)0);
		index_t len = size();
		LPCWSTR p = ToUnicode();
		index_t count = 0;
		index_t lasti = 0;
		for (index_t i = 0; i < len; ++i)
		{
			index_t j;
			for (j = 0; j < chs->size(); ++j)
				if (p[i] == chs[j])break;
			if (j < chs->size())
			{
				if (removeEmpty && lasti == i)
				{
					lasti = i + 1;
					continue;
				}
				if (count + 1 < maxCount)++count;
				else break;
				lasti = i + 1;
			}
		}
		PStringArray  strs(count + 1);
		ptr<String>* pstrs = strs;
		count = 0;
		lasti = 0;
		for (index_t i = 0; i < len; ++i)
		{
			index_t j;
			for (j = 0; j < chs->size(); ++j)
				if (p[i] == chs[j])break;
			if (j<chs->size())
			{
				if (!removeEmpty || i>lasti)
				{
					pstrs[count] = Substring(lasti, i - lasti);
					++count;
				}
				lasti = i + 1;
				if (count >= maxCount)
					break;
			}
		}
		if (count<maxCount && (!removeEmpty || len>lasti))
		{
			pstrs[count] = Substring(lasti, len - lasti);
			strs->Resize(count + 1);
		}
		else strs->Resize(count);
		return strs;
	}
	//返回使用指定字符串做分隔符分割形成的字符串数组,并指定最大数量,并指明是否移除空字符串
	PStringArray  ptr<String>::Split(PStringArray  strs, index_t maxCount, bool removeEmpty)const
	{
		if (*this==null)Throw<PPointerIsNullException >();
		if (maxCount < 0)Throw<PInvalidArgumentException>();//参数无效
		if (!size() || maxCount == 0)return PStringArray ((index_t)0);
		index_t len = size();
		LPCWSTR p = ToUnicode();

		index_t count = 0;//分隔字符串数量（如果removeEmpty为true则连续的分隔符表示1个分隔符）
		index_t lasti = 0;//上一个分隔符末尾+1的位置
		for (index_t i = 0; i < len;)
		{
			index_t j;
			for (j = 0; j < strs->size(); ++j)
				if (strs[j]->Length && i + strs[j]->Length < len && !String::memcmp(i + p, strs[j].ToUnicode(), strs[j]->Length))break;
			if (j < strs->size())
			{
				if (removeEmpty && lasti == i)
				{
					i += strs[j]->Length;
					lasti = i;
					continue;
				}
				if (count + 1 < maxCount)++count;
				else break;
				i += strs[j]->Length;
				lasti = i;
			}
			else ++i;
		}
		PStringArray  tstrs(count + 1);
		ptr<String>* pstrs = tstrs;
		count = 0;
		lasti = 0;
		for (index_t i = 0; i < len;)
		{
			index_t j;
			for (j = 0; j < strs->size(); ++j)
				if (strs[j]->Length && i + strs[j]->Length < len && !String::memcmp(i + p, strs[j].ToUnicode(), strs[j]->Length))break;
			if (j<strs->size())
			{
				if (!removeEmpty || i>lasti)
				{
					pstrs[count] = Substring(lasti, i - lasti);
					++count;
				}
				i += strs[j]->Length;
				lasti = i;
				if (count >= maxCount)
					break;
			}
			else ++i;
		}
		if (count<maxCount && (!removeEmpty || len>lasti))
		{
			pstrs[count] = Substring(lasti, len - lasti);
			tstrs->Resize(count + 1);
		}
		else tstrs->Resize(count);
		return tstrs;
	}
	//返回使用指定字符数组中出现的字符做分隔符分割形成的字符串数组,并指定最大数量
	PStringArray  ptr<String>::Split(PWCharArray chs, index_t maxCount)const
	{
		return Split(chs, maxCount, false);
	}
	//返回使用指定字符串做分隔符分割形成的字符串数组,并指定最大数量
	PStringArray  ptr<String>::Split(PStringArray  strs, index_t maxCount)const
	{
		return Split(strs, maxCount, false);
	}


#ifdef _CLR //托管支持
	ptr<String>::ptr(System::String^string, index_t count)//创建由count个字符string串接而成的字符串
		:ptr<Object>(string ? new String(string, count) : NULL)//引用计数自动加1
	{

	}
	System::String^ ptr<String>::ToGcString()const //定义到托管字符串的转换
	{
		if (GetPtr())return GetPtr()->ToGcString();
		return nullptr;
	}
	ptr<String>& ptr<String>::operator=(System::String^s)
	{
		return *this = ptr<String>(s);
	}
	ptr<String>::operator System::String ^ ()const
	{
		return ToGcString();
	}
	ptr<String>& ptr<String>::operator+=(System::String^ string)
	{
		return *this += ptr<String>(string);
	}
	ptr<String>& ptr<String>::operator+=(System::Object^ obj)
	{
		return *this += ptr<String>(obj->ToString());
	}
	ptr<String> operator+(ptr<String> xString, System::Object^ obj)
	{
		return ptr<String>(xString, ptr<String>(obj->ToString()));
	}
	ptr<String> operator+(System::Object^ obj, ptr<String> xString)
	{
		return ptr<String>(ptr<String>(obj->ToString()), xString);
	}
	ptr<String> operator+(ptr<String> xString, volatile const System::String^ string)
	{
		return ptr<String>(xString, ptr<String>((System::String ^)string));
	}
	ptr<String> operator+(volatile const System::String^ string, ptr<String> xString)
	{
		return ptr<String>(ptr<String>((System::String ^)string), xString);
	}
#endif

}

#endif
